Java多线程基础中

别等时光非礼了梦想. 提交于 2020-01-26 05:35:06

多线程

并发

定义:同一个对象多个线程同时操作

线程同步

线程同步

锁机制(synchronized)

1.synchronized方法
在方法名前加synchronized

public synchronized void test(){
}

2.synchronized块

synchronized(obj){
}

执行过程

死锁

产生:过多的同步造成相互不释放资源,从而相互等待,一般发生于同步中持有多个对象。
解决:不要在一个代码块中持有多个对象的锁就可以了。

并发协作(生产者消费者模式)

1.管程法:
管程法
案例:借助容器实现进程间的通信

public class test09 {
 public static void main(String[] args) {
  SynContainer container=new SynContainer();
  new Producter(container).start();
  new Customer(container).start();
 }
}
//生产者
class Producter extends Thread{
 SynContainer container;
 public Producter(SynContainer container) {
  this.container = container;
 }
 @Override
 public void run() {
  for(int i=0;i<100;i++) {
   System.out.println("生产--->"+i+"个馒头");
   container.push(new Steamedbun(i));
  }
 }
}
//消费者
class Customer extends Thread{
 SynContainer container;
 public Customer(SynContainer container) {
  this.container = container;
 }
 @Override
 public void run() {
  for(int i=0;i<1000;i++) {
   System.out.println("消费---》"+container.pop().getId()+"个馒头");
  }
 }
}
//缓冲区
class SynContainer{
 Steamedbun[] buns=new Steamedbun[10];//容器
 int count=0;//计数器
 //储存
 public synchronized void push(Steamedbun bun) {
  //判断是否可以生产
  if(count==buns.length) {
   try {
    this.wait();//消费者通知生产
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  buns[count]=bun;
  count++;
  this.notifyAll();
 }
 //获取
 public synchronized Steamedbun pop() {
  //判断是否能消费
  if(count==0) {
   try {
    this.wait();//线程阻塞,生产者通知解除阻塞
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  Steamedbun temp=new Steamedbun();
  count--;
  temp=buns[count];
  this.notifyAll();
  return temp;
 }
}
//馒头
class Steamedbun{
 private int id;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public Steamedbun() {
 }
 public Steamedbun(int id) {
  this.id=id;
 }
}

2.信号灯法:
案例:

public class test02 {
 public static void main(String[] args) {
  Tv tv=new Tv();
  new Player(tv).start();
  new Watcher(tv).start();
 }
}
//生产者-演员
class Player extends Thread{
 Tv tv=new Tv();
 public Player(Tv tv) {
  this.tv = tv;
 }
 @Override
 public void run() {
  for(int i=0;i<20;i++) {
   this.tv.play("节目"+i);
  }
 }
}
//消费者-观众
class Watcher extends Thread{
 Tv tv=new Tv();
 public Watcher(Tv tv) {
  this.tv = tv;
 }
 @Override
 public void run() {
  for(int i=0;i<20;i++) {
   this.tv.watch();
  }
 }
}
//同一资源-电视
class Tv{
 String voice;
 //信号灯
 boolean flag=true;
 //如果为真表示演员表演,观众等待。
 //如果为假表示观众观看,演员等待。
 public synchronized void play(String voice) {
  //判断是否等待
  if(!flag) {
   try {
    this.wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("表演了"+voice);
  this.voice=voice;
  //唤醒
  this.notifyAll();
  //切换标志
  this.flag=!this.flag;
 }
 public synchronized void watch() {
  //判断是否等待
    if(flag) {
     try {
      this.wait();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
  System.out.println("听到了"+voice);
  this.notifyAll();
  this.flag=!this.flag;
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!