一,介绍
本总结我对于java多线程中线程之间的通信方式的理解,主要以代码结合文字的方式来讨论线程间的通信,故摘抄了书中的一些示例代码。
二,线程间的通信方式
①同步
这里讲的同步是指多个线程通过synchronized关键字这种方式来实现线程间的通信。
参考示例:
public class myobject {
synchronized public void methoda() {
//do something....
}
synchronized public void methodb() {
//do some other thing
}
}
public class threada extends thread {
private myobject object;
//省略构造方法
@override
public void run() {
super.run();
object.methoda();
}
}
public class threadb extends thread {
private myobject object;
//省略构造方法
@override
public void run() {
super.run();
object.methodb();
}
}
public class run {
public static void main(string[] args) {
myobject object = new myobject();
//线程a与线程b 持有的是同一个对象:object
threada a = new threada(object);
threadb b = new threadb(object);
a.start();
b.start();
}
}
由于线程a和线程b持有同一个myobject类的对象object,尽管这两个线程需要调用不同的方法,但是它们是同步执行的,比如:线程b需要等待线程a执行完了methoda()方法之后,它才能执行methodb()方法。这样,线程a和线程b就实现了 通信。
这种方式,本质上就是“共享内存”式的通信。多个线程需要访问同一个共享变量,谁拿到了锁(获得了访问权限),谁就可以执行。
②while轮询的方式
代码如下:
import java.util.arraylist;
import java.util.list;
public class mylist {
private list list = new arraylist();
public void add() {
list.add("elements");
}
public int size() {
return list.size();
}
}
import mylist.mylist;
public class threada extends thread {
private mylist list;
public threada(mylist list) {
super();
this.list = list;
}
@override
public void run() {
try {
for (int i = 0; i < 10; i++) {
list.add();
system.out.println("添加了" + (i + 1) + "个元素");
thread.sleep(1000);
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
import mylist.mylist;
public class threadb extends thread {
private mylist list;
public threadb(mylist list) {
super();
this.list = list;
}
@override
public void run() {
try {
while (true) {
if (list.size() == 5) {
system.out.println("==5, 线程b准备退出了");
throw new interruptedexception();
}
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
import mylist.mylist;
import extthread.threada;
import extthread.threadb;
public class test {
public static void main(string[] args) {
mylist service = new mylist();
threada a = new threada(service);
a.setname("a");
a.start();
threadb b = new threadb(service);
b.setname("b");
b.start();
}
}
在这种方式下,线程a不断地改变条件,线程threadb不停地通过while语句检测这个条件(list.size()==5)是否成立 ,从而实现了线程间的通信。但是这种方式会浪费cpu资源。之所以说它浪费资源,是因为jvm调度器将cpu交给线程b执行时,它没做啥“有用”的工作,只是在不断地测试 某个条件是否成立。就类似于现实生活中,某个人一直看着手机屏幕是否有电话来了,而不是: 在干别的事情,当有电话来时,响铃通知ta电话来了。
③wait/notify机制
代码如下:
import java.util.arraylist;
import java.util.list;
public class mylist {
private static list list = new arraylist();
public static void add() {
list.add("anystring");
}
public static int size() {
return list.size();
}
}
public class threada extends thread {
private object lock;
public threada(object lock) {
super();
this.lock = lock;
}
@override
public void run() {
try {
synchronized (lock) {
if (mylist.size() != 5) {
system.out.println("wait begin "
+ system.currenttimemillis());
lock.wait();
system.out.println("wait end "
+ system.currenttimemillis());
}
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
public class threadb extends thread {
private object lock;
public threadb(object lock) {
super();
this.lock = lock;
}
@override
public void run() {
try {
synchronized (lock) {
for (int i = 0; i < 10; i++) {
mylist.add();
if (mylist.size() == 5) {
lock.notify();
system.out.println("已经发出了通知");
}
system.out.println("添加了" + (i + 1) + "个元素!");
thread.sleep(1000);
}
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
public class run {
public static void main(string[] args) {
try {
object lock = new object();
threada a = new threada(lock);
a.start();
thread.sleep(50);
threadb b = new threadb(lock);
b.start();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
线程a要等待某个条件满足时(list.size()==5),才执行操作。线程b则向list中添加元素,改变list 的size。
a,b之间如何通信的呢?也就是说,线程a如何知道 list.size() 已经为5了呢?
这里用到了object类的 wait() 和 notify() 方法。
当条件未满足时(list.size() !=5),线程a调用wait() 放弃cpu,并进入阻塞状态。---不像②while轮询那样占用cpu
当条件满足时,线程b调用 notify()通知 线程a,所谓通知线程a,就是唤醒线程a,并让它进入可运行状态。
这种方式的一个好处就是cpu的利用率提高了。
但是也有一些缺点:比如,线程b先执行,一下子添加了5个元素并调用了notify()发送了通知,而此时线程a还执行;当线程a执行并调用wait()时,那它永远就不可能被唤醒了。因为,线程b已经发了通知了,以后不再发通知了。这说明:通知过早,会打乱程序的执行逻辑。
以上这篇深入理解java多线程之线程间的通信方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!