java multithreading - thread alternately

  Claim:

  By means of the synchronization mechanism, SLEEP () method, the Join () method to realize animation display;

  A thread: 1,3,5,7,9

  B Thread: 2,4,6,8,10

  Propan thread: a, b, c, d, e

  main () thread Output: thread starts, the thread end

  Output: the thread starting, 1-a-2 ## 3-b-4 ## 5-c-6 ## ...

  Thinking:

  Determining a plurality of markers, Analog (consumer - producer) outputs each thread after a wait, and then change their marks

  Critical resource - a plurality == putX () == method determines own marker (== isEmptyX ==) then output

  Enabling multiple threads execute orderly alternately

  Code:

  class Resource{

  private boolean isEmpty01 = true;

  private boolean isEmpty02 = false;

  private boolean isEmpty03 = false;

  // put each method corresponds to one output, each output on a waiting, waiting awaken others

  public void put1(){

  while(!isEmpty01){

  try{

  wait();

  }catch(InterruptedException e){

  e.printStackTrace ();

  }

  }

  After // output

  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

  // wrong point

  // 61 and 62, these two can not be swapped

  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;

  } Wuxi well-known and Women's Hospital 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();

  }

  }

  Produce the results.

  It is important:

  Such markers may be implemented using more than two threads of execution ordered alternately


Guess you like

Origin blog.51cto.com/14335413/2402404