欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-02 09:29:46
死锁定义 死锁是指两个或者多个线程被永久阻塞的一种局面,产生的前提是要有两个或两个以上的线程,并且来操作两个或者多个以上的共同资源;我的理解是用两个线程来举例,现有...

死锁定义

死锁是指两个或者多个线程被永久阻塞的一种局面,产生的前提是要有两个或两个以上的线程,并且来操作两个或者多个以上的共同资源;我的理解是用两个线程来举例,现有线程a和b同时操作两个共同资源a和b,a操作a的时候上锁locka,继续执行的时候,a还需要lockb进行下面的操作,这个时候b资源在被b线程操作,刚好被上了锁lockb,假如此时线程b刚好释放了lockb则没有问题,但没有释放lockb锁的时候,线程a和b形成了对lockb锁资源的争夺,从而造成阻塞,形成死锁;具体其死锁代码如下:

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");
     }
    }
   }
 }
}

执行结果如下图:

thread-1 acquie lock is obj
thread-0 acquie lock is deadres.class

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

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

1、让两个线程交替进行操作,当生产了一个数字后,紧接着消费一个,首先采用object对象中的wait-notify来实现,具体代码如下:

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(),这样多个线程操作的时候,便可以交替进行,具体代码如下:

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

2、采用lock-condition方法实现如下:

class product{
 string name;
 private int count = 1;
 boolean flag = false;
 lock lock = new reentrantlock();
 condition conditon = lock.newcondition();
 public void set(string name){
  try{
   lock.lock();
   while(flag){
    conditon.await();
   }
   this.name = name +"--"+count++;
   flag = true;
   system.out.println(thread.currentthread().getname()+" produce num : "+this.name);
   conditon.signalall();
  }catch (exception e){
  }finally {
   lock.unlock();
  }
 }
 public void out(){
  try{
   lock.lock();
   while(!flag){
    conditon.await();
   }
   flag = false;
   system.out.println(thread.currentthread().getname()+" consumer num is : "+this.name);
   conditon.signalall();
  }catch (exception e){
  }finally {
   lock.unlock();
  }
 }
}

执行结果如下:

thread-0 produce num : guyue--20305
thread-3 consumer num is : guyue--20305
thread-1 produce num : guyue--20306
thread-2 consumer num is : guyue--20306
thread-0 produce num : guyue--20307
thread-3 consumer num is : guyue--20307
thread-1 produce num : guyue--20308
thread-2 consumer num is : guyue--20308

以上就是本文关于java编程之多线程死锁与线程间通信简单实现代码的全部内容,希望对大家有所帮助。关于java多线程以及线程间通信的例子,本站还有几篇文章可以参考:

java多线程编程小实例模拟停车场系统java网络编程基础篇之单向通信

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!