Detailed explanation of Java multithreaded communication implementation
This article mainly introduces the detailed implementation of Java multi-threaded communication. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
Thread communication mode:
1. Shared variable
Inter thread communication can send signals. A simple way to send signals is to set the signal value in the variable of the shared object. Thread a sets the Boolean member variable hasdatatoprocess to true in a synchronization block, and thread B also reads the member variable hasdatatoprocess in the synchronization code block. This simple example uses an object that holds a signal and provides set and get methods.
public class MySignal1 {
//共享的变量
private boolean hasDataToProcess = false;
//取值
public boolean getHasDataProcess() {
return hasDataToProcess;
}
//存值
public void setHasDataToProcess(boolean hasDataToProcess) {
this.hasDataToProcess = hasDataToProcess;
}
public static void main(String[] args) {
//同一个对象
final MySignal1 my = new MySignal1();
//线程1设置hasDataToProcess值为true
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
my.setHasDataToProcess(true);
}
});
t1.start();
//线程2取这个值hasDataToProcess
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
//等待线程1完成后取值
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
my.getHasDataProcess();
System.out.println("t1改变以后的值:"+my.getHasDataProcess());
}
});
t2.start();
}
}
The operation results are as follows:
Value after T1 change: true
2. Wait / notify mechanism
Taking resources as an example, a producer produces a resource and informs consumers to consume a resource. The producer continues to produce resources and consumers consume resources. This cycle is coded as follows.
import sun.security.util.Password;
//资源类
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name) {
//生产资源
while (flag) {
try {
//线程等待
wait();
} catch (InterruptedException e) {
}
}
this.name = name + "----" + count + "+++++";
System.out.println(Thread.currentThread().getName() + "..生产者..." + this.name);
flag = true;
//唤醒等待中的消费者
this.notifyAll();
}
public synchronized void out() {
//消费资源
while (!flag) {
try {
//线程等待,生产者生产资源
wait();
} catch (InterruptedException e) {
}
}
System.out.println(Thread.currentThread().getName() + "...消费者..." + this.name);
flag = false;
//唤醒消费者,生产资源
this.notifyAll();
}
}
//生产者
class Producer implements Runnable {
private Resource rs;
public Producer(Resource rs) {
this.rs = rs;
}
//生产者生产资源
@Override
public void run() {
while (true) {
rs.set("商品");
}
}
}
//消费者消费资源
class Consumer implements Runnable {
private Resource rs;
public Consumer(Resource rs) {
this.rs = rs;
}
//消费者消费资源
@Override
public void run() {
while (true) {
rs.out();
}
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource r = new Resource();
Producer p = new Producer(r);
Consumer c = new Consumer(r);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
The operation results are as follows:
Thread-0..生产者...商品----1+++++ Thread-1...消费者...商品----1+++++ Thread-0..生产者...商品----1+++++ Thread-1...消费者...商品----1+++++ Thread-0..生产者...商品----1+++++ Thread-1...消费者...商品----1+++++ Thread-0..生产者...商品----1+++++
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.
