您的位置:首页 > 其它

线程的互斥与同步通信 - 例子:子主线程打印,子主线程互补干扰问题

2015-12-30 14:35 302 查看
一. 互斥

  结局java线程中的互斥问题的的方法

   1.同步代码块,将敏感数据或者敏感操作放在同步代码块里,args代表一个参照,注意:多个方法调用同步代码块需要保证参照相同

synchronized (args) {
  // Code
}


   ※ args:可以是对象,可以是属性,也可以是类的字节码(类的字节码Object.class,jvm中唯一) 

    2.synchronized作为方法的修饰符

public synchronized void sync(int i){
System.out.println(" message ");
}


二. 同步通信

  同步通信使用Object提供的三个方法wait ,notify和notfiyAll三个方法,简单的理解为线程之间的通信,一个线程通知另一个线程

三. JAVA一个经典习题

  子线程循环10次,紧接着主线程循环100次,紧接着子线程循环10次,主线程100次,如此往复50次

  

public class TraditionalThreadTest {

public static void main(String[] args) {    //主线程
final BusinessTest b = new BusinessTest();
new Thread(new Runnable() {    //子线程
@Override
public void run() {
for (int i = 1; i <= 50; i++) {
b.sub(i);
}
}
}).start();

for (int i = 1; i <= 50; i++) {
b.main(i);
}

}

}
class BusinessTest{
private boolean flag = true;
public synchronized void sub(int i){
while (!flag) {    //true 執行
try {
this.wait();    //false是等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++) {
System.out.println("子线程 : "+ j + ",第"+ i +"次打印");
}
flag = false;
this.notify();    //通知下一个线程
System.out.println();
}
public synchronized void main(int i){
while (flag) {    //false 執行
try {
this.wait();    //true 等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++) {
System.out.println("主线程 : "+ j + ",第"+ i +"次打印");
}
flag = true;
this.notify();    //通知下一个线程
System.out.println();
}
}


 实现原理:利用线程之间的通信获取flag的值,来确定是不是应该执行或者是应该停止

    注意这里的判断使用的是while而不是if,使用while的好处是可以自检
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: