线程之间通信的方法有四种:
1. 共享内存的方法
该方法有两种实现形式:
synchronized和volatile
使用synchronized,多线程环境下会造成阻塞,如果有多个线程同时要对内存中的一个数据进行操作,那么他们都把内存中的值读到自己的工作内存中。使用synchronized修饰的haul同一时间只能有一个线程进行操作,对数据更新完之后重新刷新会主存,自动释放锁,然后下一个线程来读取内存中的值,实现线程通信
使用volatile:用它修饰的变量线程在对其进行操作时,数据更新完之后直接刷新会主存,其他线程读的话,即使通过内存有数据,也不读,读的是主内存中的值,从而实现通信
+wait+notify(同步,等待,唤醒)
在生产者消费者模式中,生产者生产到库存满了以后就不能再生产,调用wait方法挂起,然后在调用notify方法唤醒消费者进行消费,可以实现线程之间的通信
举例:
**
* 资源类,表示生产者和消费者操作的资源
*/
public class Resource {
private int num=0;
private int size=10;
public synchronized void put()
{
while (num>=size)
{
("生产者进入等待...");
try {
();
} catch (InterruptedException e) {
();
}
}
num++;
("当前线程是:"+().getName()+"资源数量:"+num);
notifyAll();
}
public synchronized void remove()
{
while (num<=0)
{
("消费者进入等待");
try {
();
} catch (InterruptedException e) {
();
}
}
num--;
("当前线程:"+().getName()+"资源数量"+num);
notifyAll();
}
}
public class Productor implements Runnable{
private Resource resource;
public Productor(Resource resource) {
= resource;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
();
try {
(100);
} catch (InterruptedException e) {
();
}
}
}
}
public class Consumer implements Runnable {
private Resource resource;
public Consumer(Resource resource) {
= resource;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
();
try {
(10);
} catch (InterruptedException e) {
();
}
}
}
}
public class Test {
public static void main(String[] args) {
Resource resource=new Resource();
Consumer consumer=new Consumer(resource);
Productor productor=new Productor(resource);
new Thread(consumer).start();
new Thread(productor).start();
}
}
+condition+await+singal 实现
一般情况下使用ReentrantLock
举例:
public class Resource {
private int num=0;
private int size=10;
private ReentrantLock lock=new ReentrantLock();
private Condition condition=();
public void put()
{
();
while (num>=size)
{
try {
("生产者进入等待状态...");
();
} catch (InterruptedException e) {
();
}
}
num++;
("当前线程是:"+().getName()+"当前资源数量:"+num);
();
();
}
public void remove()
{
();
while(num<=0)
{
try {
("消费者进入等待状态...");
();
} catch (InterruptedException e) {
();
}
}
num--;
("当前线程是:"+().getName()+"资源数量:"+num);
();
();
}
}
public class Productor implements Runnable{
private Resource resource;
public Productor(Resource resource) {
= resource;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
();
try {
(100);
} catch (InterruptedException e) {
();
}
}
}
}
public class Consumer implements Runnable {
private Resource resource;
public Consumer(Resource resource) {
= resource;
}
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
();
try {
(10);
} catch (InterruptedException e) {
();
}
}
}
}
public class Test {
public static void main(String[] args) {
Resource resources = new Resource();
Consumer consumer = new Consumer(resources);
Productor productor=new Productor(resources);
();
new Thread(consumer).start();
new Thread(productor).start();
}
}
4.管道间通信
在两个线程之间建立一个通道,就是pipeInputStream和pipeOutputStream,inputstream用户接受信息,outputstream用户发送信息。
举例如下:
public class ReadData {
public void readMethod(PipedInputStream input) {
("read");
byte[] byteArray = new byte[20];
try {
int readLength = (byteArray);
while (readLength != -1) {
String newData = new String(byteArray, 0, readLength);
(newData);
readLength = (byteArray);
}
} catch (IOException e) {
();
}
}
}
public class WriteData {
public void writeMethod(PipedOutputStream out) {
("write:");
try {
for (int i = 0; i < 300; i++) {
String outData = "" + (i + 1);
(());
(outData);
}
();
();
} catch (IOException e) {
();
}
}
}
public class ThreadRead extends Thread{
private ReadData readData;
private PipedInputStream inputStream;
public ThreadRead(ReadData readData,PipedInputStream inputStream) {
= readData;
=inputStream;
}
@Override
public void run()
{
(inputStream);
}
}
public class ThreadWrite extends Thread {
private WriteData writeData;
private PipedOutputStream out;
public ThreadWrite(WriteData writeData, PipedOutputStream out) {
= writeData;
= out;
}
@Override
public void run()
{
(out);
}
}
public class Test {
public static void main(String[] args) {
try {
WriteData writeData = new WriteData();
ReadData readData = new ReadData();
PipedInputStream inputStream = new PipedInputStream();
PipedOutputStream outputStream = new PipedOutputStream();
(inputStream);
ThreadRead threadRead=new ThreadRead(readData,inputStream);
();
try {
(2000);
} catch (InterruptedException e) {
();
}
ThreadWrite threadWrite=new ThreadWrite(writeData,outputStream);
();
} catch (IOException e) {
();
}
}
}
以上是关于线程之间通信的方法,希望对你有用