14.10.4 交换器
Exchanger 可用于交换两个线程的数据,用法如下:
Exchanger<T> c = new Exchanger<>();//创建指定交换类型的交换器
int v2 = c.exchange(v)//把数据v用于线程交换,同时获取对方数据v2
具体如下:
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import java.util.Random;
import java.util.concurrent.Exchanger;
public class Main{
public static void main(String[] args) throws InterruptedException {
Main solution = new Main();
Exchanger<Integer> exchanger = new Exchanger<>();
for(int i = 0; i < 2; i++){
Thread t = new Thread(new A(exchanger,"线程"+i));
t.start();
}
}
}
class A implements Runnable{
private Exchanger<Integer> changer;
private String name;
private Random r = new Random();
public A(Exchanger changer,String name){
this.changer = changer;
this.name = name;
}
@Override
public void run() {
try {
int v = r.nextInt(100);
System.out.println(name+"获得的随机数为:"+v);
Integer otherInter = changer.exchange(v);
System.out.println(name+"得到对方的随机数为:"+otherInter);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
执行结果:
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
14.10.5 同步队列
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import java.util.Random;
import java.util.concurrent.SynchronousQueue;
public class Main{
public static void main(String[] args) throws InterruptedException {
Main solution = new Main();
SynchronousQueue<Integer> queue = new SynchronousQueue<Integer>();
Thread a = new Thread(new A(queue,"线程1"));
a.start();
Thread b = new Thread(new B(queue,"线程2"));
b.start();
}
}
class A implements Runnable{
private SynchronousQueue<Integer> queue;
private String name;
private Random r = new Random();
public A(SynchronousQueue<Integer> queue,String name){
this.queue = queue;
this.name = name;
}
@Override
public void run() {
try {
int v = r.nextInt(100);
System.out.println(name+"获得的随机数为:"+v);
queue.put(v);
System.out.println(name+"执行结束");
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
class B implements Runnable{
private SynchronousQueue<Integer> queue;
private String name;
public B(SynchronousQueue<Integer> queue,String name){
this.queue = queue;
this.name = name;
}
@Override
public void run() {
try {
int v = queue.take();
System.out.println(name+"获得的随机数为:"+v);
System.out.println(name+"执行结束");
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
执行结果:
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
14.10.6 暂停恢复动画
此示例为书作者实例,说明如下:
🌮 随机绘制一些高度不同,宽度相同的长方形
🌮 使用Comporator进行高度排序
🌮 如果是按步骤的情况,需要获取通行证,则下一次比较获取通行证时会阻塞
🌮 如果是连续运行的情况,则每次绘制完成后停止一段时间,下一次继续绘制(由于没有占获取通行证的操作,不会造成后续步骤的阻塞)
🌮 比较后,长方形会根据比较器结果进行重新绘制,最终得到的结果,再全部绘制一次,则得到从短到长的长方形高度排列
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.Semaphore;
public class AlgorithmAnimation {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new AnimationFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class AnimationFrame extends JFrame{
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
public AnimationFrame(){
ArrayComponent comp = new ArrayComponent();
add(comp,BorderLayout.CENTER);
final Sorter sorter = new Sorter(comp);
JButton runButton = new JButton("Run");
runButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sorter.setRun();
}
});
JButton stepButton = new JButton("step");
stepButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sorter.setStep();
}
});
JPanel buttons = new JPanel();
buttons.add(runButton);
buttons.add(stepButton);
add(buttons,BorderLayout.NORTH);
setSize(WIDTH,HEIGHT);
Thread t = new Thread(sorter);
t.start();
}
}
class Sorter implements Runnable{
private Double[] values;
private ArrayComponent component;
private Semaphore gate;
private static final int DELAY = 100;
private volatile boolean run;
private static final int VALUES_LENGTH = 30;
public Sorter(ArrayComponent comp){
values = new Double[VALUES_LENGTH];
for(int i = 0; i < values.length; i++){
values[i] = new Double(Math.random());
}
this.component = comp;
this.gate = new Semaphore(1);
this.run = false;
}
public void setRun(){
run = true;
gate.release();
}
public void setStep(){
run = false;
gate.release();
}
public void run(){
Comparator<Double> comp = new Comparator<Double>(){
@Override
public int compare(Double i1, Double i2) {
component.setValues(values,i1,i2);
try{
if(run) Thread.sleep(DELAY);
else gate.acquire();
}catch (InterruptedException exception){
Thread.currentThread().interrupt();
}
return i1.compareTo(i2);
}
};
Arrays.sort(values,comp);
component.setValues(values,null,null);
}
}
class ArrayComponent extends JComponent {
private Double marked1;
private Double marked2;
private Double[] values;
public synchronized void setValues(Double[] values, Double marked1, Double marked2){
this.marked1 = marked1;
this.values = values.clone();
this.marked2 = marked2;
repaint();
}
public synchronized void paintComponent(Graphics g){
if(values == null) return;
Graphics2D g2 = (Graphics2D) g;
int width = getWidth()/values.length;
for(int i = 0; i < values.length; i++){
double height = values[i]*getHeight();
Rectangle2D bar = new Rectangle2D.Double(width*i,0,width,height);
if(values[i]==marked1 || values[i]==marked2)
g2.fill(bar);
else
g2.draw(bar);
}
}
}
❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤
执行结果:
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍
相关内容:选择 《Java核心技术 卷1》查找相关笔记
评论🌹点赞👍收藏✨关注👀,是送给作者最好的礼物,愿我们共同学习,一起进步
公众号 钰娘娘知识汇总