b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Java编程之多线程死锁与线程间通信简单实现代码

电脑杂谈  发布时间:2019-06-27 22:04:39  来源:网络整理

c++ 线程死锁_java多线程死锁_java线程死锁例子

死锁定义

如果释放互斥量锁时有多个线程阻塞,所有阻塞在该互斥量的线程都会变成可运行状态,其中第一个变成可运行状态的线程获得互斥量的锁,其他线程会再次阻塞。这里篮子是一个互斥区,每次放鸡蛋是互斥的,每次取鸡蛋也是互斥的,a线程放鸡蛋,如果这时b线程要取鸡蛋,由于a没有释放锁,b线程处于等待状态,进入阻塞队列,放鸡蛋之后,要通知b线程取鸡蛋,b线程进入就绪队列,反过来,b线程取鸡蛋,如果a线程要放 鸡蛋,由于b线程没有释放锁,a线程处于等待状态,进入阻塞队列,取鸡蛋之后,要通知a线程放鸡蛋,a线程进入就绪队列。对互斥量加锁后,其他线程试图对互斥量加锁时都会被阻塞直到互斥量的锁释放(注意如果线程试图对同一个互斥量加锁两次,那么它自身就会陷入阻塞,即死锁状态。

public class MyDeadLockTest {
 public static void main(String[] args){
  Object obj1 = new Object();
  Thread thread1 = new Thread(new DeadRes(true,obj1));
  Thread thread2 = new Thread(new DeadRes(false,obj1));
  thread1.start();
  thread2.start();
 }
}
class DeadRes implements Runnable{
 boolean flag;
 Object obj;
 public DeadRes(boolean flag, Object obj1) {
  this.flag = flag;
  this.obj = obj1;
 }
 @Override
 public void run() {
   if(flag){
    synchronized (DeadRes.class){
     System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class");
     synchronized (obj){
      System.out.println(Thread.currentThread().getName()+" acquie lock is obj");
     }
    }
   }else{
    synchronized (obj){
     System.out.println(Thread.currentThread().getName()+" acquie lock is obj");
     synchronized (DeadRes.class){
      System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class");
     }
    }
   }
 }
}

执行结果如下图:

java多线程死锁_java线程死锁例子_c++ 线程死锁

Thread-1 acquie lock is obj
Thread-0 acquie lock is DeadRes.class

当然每次执行的结果不一样,有可能是一种和谐状态,没有发生死锁,此时为保证每次死锁,可以让run()方法中,执行while(true)循环,这样保证了每次必定发生死锁;当然实际应用中java多线程死锁,我们应该尽量避免死锁,当有多线程操作多个共同资源的时候,避免发生同一锁对象的同步嵌套。

线程间的通讯—-生产者与消费者模式

brian goetz对线程安全比较恰当的定义:当多个线程访问一个对象时,如果不考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调度方进行任何其他的协调操作,调用这个对象的行为都可以获得正确的结果,那这个对象就是线程安全的。上述代码与前面不同的是我们同时创建了两个新实例accountingsyncbad,然后启动两个不同的线程对共享变量i进行操作,但很遗憾操作结果是1452317而不是期望结果2000000,因为上述代码犯了严重的错误,虽然我们使用synchronized修饰了increase方法,但却new了两个不同的实例对象,这也就意味着存在着两个不同的实例对象锁,因此t1和t2都会进入各自的对象锁,也就是说t1和t2线程使用的是不同的锁,因此线程安全是无法保证的。但是如果有多个生产者和多个消费者,上面的代码是有问题,比如2个生产者,2个消费者,运行结果就可能出现生产的1个商品生产了一次而被消费了2次,或者连续生产2个商品而只有1个被消费,这是因为此时共有4个线程在操作resource对象r, 而notify()唤醒的是线程池中第1个wait()的线程,所以生产者执行notify()时,唤醒的线程有可能是另1个生产者线程,这个生产者线程从wait()中醒来后不会再判断flag,而是直接向下运行打印出一个新的商品,这样就出现了连续生产2个商品。

java线程死锁例子_java多线程死锁_c++ 线程死锁

public class ThreadProConsume {
 public static void main(String[] args){
  Product product = new Product();
  Thread thread1 = new Thread(new Producer(product));
  Thread thread2 = new Thread(new Consumer(product));
  thread1.start();
  thread2.start();
 }
}
class Product{
 String name;
 private int count = 1;
 boolean flag = false;
 public synchronized void set(String name){
  if(flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.name = name +"--"+count++;
  flag = true;
  System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
  this.notify();
 }
 public synchronized void out(){
  if(!flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name);
  flag = false;
  this.notify();
 }
}
class Producer implements Runnable{
 Product res;
 public Producer(Product product) {
  this.res = product;
 }
 @Override
 public void run() {
  while(true){
   res.set("guyue");
  }
 }
}
class Consumer implements Runnable{
 Product res;
 public Consumer(Product product) {
  this.res = product;
 }
 @Override
 public void run() {
  while(true){
   res.out();
  }
 }
}

执行结果如图:

Thread-1 consume num is : guyue--3938
Thread-0 produce num : guyue--3939
Thread-1 consume num is : guyue--3939
Thread-0 produce num : guyue--3940
Thread-1 consume num is : guyue--3940
Thread-0 produce num : guyue--3941
Thread-1 consume num is : guyue--3941

当超过两个以上线程操作的时候,这里需要在set()与out()方法中的if判断改为while,并且notif方法,改为notifyAll(),这样多个线程操作的时候,便可以交替进行,具体代码如下:

java多线程死锁_c++ 线程死锁_java线程死锁例子

public class ThreadProConsume {
 public static void main(String[] args){
  Product product = new Product();
  Thread thread1 = new Thread(new Producer(product));
  Thread thread3 = new Thread(new Producer(product));
  Thread thread2 = new Thread(new Consumer(product));
  Thread thread4 = new Thread(new Consumer(product));
  thread1.start();
  thread3.start();
  thread2.start();
  thread4.start();
 }
}
class Product{
 String name;
 private int count = 1;
 boolean flag = false;
 public synchronized void set(String name){
  while(flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.name = name +"--"+count++;
  flag = true;
  System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
  this.notifyAll();
 }
 public synchronized void out(){
  while (!flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name);
  flag = false;
  this.notifyAll();
 }
}

执行结果如下:

Thread-0 produce num : guyue--50325
Thread-2 consume num is : guyue--50325
Thread-1 produce num : guyue--50326
Thread-3 consume num is : guyue--50326
Thread-0 produce num : guyue--50327
Thread-2 consume num is : guyue--50327
Thread-1 produce num : guyue--50328
Thread-3 consume num is : guyue--50328


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-108877-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...