溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java 多線程–線程交替

發(fā)布時間:2020-06-17 21:58:41 來源:網(wǎng)絡 閱讀:289 作者:nineteens 欄目:編程語言

  要求:

  借助同步機制,sleep()方法,join()方法,實現(xiàn)動畫顯示;

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

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

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

  main()線程輸出:線程開始,線程結(jié)束

  輸出結(jié)果:線程開始,1-a-2## 3-b-4## 5-c-6## …

  思考:

  使用多個判斷標記,模擬(消費者-生產(chǎn)者)每線程輸出一個后就等待,然后改變自己的標記

  臨界資源–使用多個== putX() == 方法,判斷屬于自己的標記(== isEmptyX ==)然后輸出

  使多個線程有序的交替執(zhí)行

  代碼:

  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;

  }無錫知名的婦科醫(yī)院 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();

  }

  }

  產(chǎn)生執(zhí)行結(jié)果。

  重要的是:

  這種利用標記可以實現(xiàn)超過2個線程的有序交替執(zhí)行


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI