温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java 多线程–线程交替

发布时间:2020-06-17 21:58:41 来源:网络 阅读:279 作者:nineteens 栏目:编程语言

  要求:

  借助同步机制,sleep()方法,join()方法,实现动画显示;

  甲线程:1、3、5、7、9

  乙线程:2、4、6、8、10

  丙线程:a、b、c、d、e

  main()线程输出:线程开始,线程结束

  输出结果:线程开始,1-a-2## 3-b-4## 5-c-6## …

  思考:

  使用多个判断标记,模拟(消费者-生产者)每线程输出一个后就等待,然后改变自己的标记

  临界资源–使用多个== putX() == 方法,判断属于自己的标记(== isEmptyX ==)然后输出

  使多个线程有序的交替执行

  代码:

  class Resource{

  private boolean isEmpty01 = true;

  private boolean isEmpty02 = false;

  private boolean isEmpty03 = false;

  //每个put方法对应一个输出,每输出一个就等待,等待其他人的唤醒

  public void put1(){

  while(!isEmpty01){

  try{

  wait();

  }catch(InterruptedException e){

  e.printStackTrace();

  }

  }

  //输出后

  isEmpty01 = false;

  isEmpty02 = true;

  notifyAll();

  }

  public void put2(){

  while(!isEmpty02){

  try{

  wait();

  }catch(InterruptedException e){

  e.printStackTrace();

  }

  }

  isEmpty02 = false;

  isEmpty03 = true;

  notifyAll();

  }

  public void put3(){

  while(!isEmpty03){

  try{

  wait();

  }catch(InterruptedException e){

  e.printStackTrace();

  }

  }

  isEmpty03 = false;

  isEmpty01 = true;

  notifyAll();

  }

  }

  class Player01 implements Runnable{

  private Resource res;

  private String[] arr;

  Player01(){}

  Player01(String[] arr,Resource res){

  this.arr = arr;

  this.res = res;

  }

  public void run(){

  synchronized(res){

  for(int i=0;i

  //错误的点

  //61,62,这两句不能交换顺序

  res.put1();

  System.out.print(arr[i]+"-");

  try{

  Thread.sleep(1000);

  }catch(InterruptedException e){

  e.printStackTrace();

  }

  }

  }

  }

  }

  class Player02 implements Runnable{

  private Resource res;

  private String[] arr;

  Player02(){}

  Player02(String[] arr,Resource res){

  this.arr = arr;

  this.res = res;

  }

  public void run(){

  synchronized(res){

  for(int i=0;i

  res.put2();

  System.out.print(arr[i]+"-");

  try{

  Thread.sleep(1000);

  }catch(InterruptedException e){

  e.printStackTrace();

  }

  }

  }

  }

  }

  class Player03 implements Runnable{

  private Resource res;

  private String[] arr;

  Player03(){}

  Player03(String[] arr,Resource res){

  this.arr = arr;

  this.res = res;

  }无锡知名的妇科医院 http://www.wxbhnkyy39.com/

  public void run(){

  synchronized(res){

  for(int i=0;i

  res.put3();

  System.out.print(arr[i]+"## ");

  try{

  Thread.sleep(1000);

  }catch(InterruptedException e){

  e.printStackTrace();

  }

  }

  }

  }

  }

  class Test08{

  public static void main(String[] args){

  String[] arr1 = {"1","3","5","7","9"};

  String[] arr2 = {"a","b","c","d","e"};

  String[] arr3 = {"2","4","6","8","0"};

  Resource res = new Resource();

  Player01 p1 = new Player01(arr1,res);

  Player02 p2 = new Player02(arr2,res);

  Player03 p3 = new Player03(arr3,res);

  Thread t1 = new Thread(p1);

  Thread t2 = new Thread(p2);

  Thread t3 = new Thread(p3);

  t1.start();

  t2.start();

  t3.start();

  }

  }

  产生执行结果。

  重要的是:

  这种利用标记可以实现超过2个线程的有序交替执行


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI