利用线程通信打印12A34B。。。

方法1:
package threadcommunication;
/**
 * 利用线程通信原理
 * 打印12A34B56C78D。。。。5152Z
 *
 * @author think
 */
public class Print {
 
 public boolean flag = true;
 public synchronized void printNum(){
  for(int i = 1;i<=26;i++){
   if(!flag){
    try {
     wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   System.out.print((2*i-1)+""+(2*i));
   flag = false;
   notifyAll();
  }
 }
 
 public synchronized void printChar(){
  for(char i='A';i<='Z';i++){//for循环不能让其停掉
   if(flag){
    try {
     wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   //如果要连续打印循环中的i的话,千万注意不能加else!!!加的话,flag为true时,程序wait,到被notify()之后,此处循环中else的代码走不到
   //不加else的话,被notify()之后跳过if()还会向下走,打印此处循环中的i
//   else{//
    System.out.print(i);
    flag = true;
    notifyAll();
//   }
  }
 }
 public static void main(String[] args) {
  Print p = new Print();
  Thread t1 = new Thread(){
   public void run(){
    p.printNum();
   }
  };
  Thread t2 = new Thread(){
   public void run(){
    p.printChar();
   }
  };
  t1.start();
  t2.start();
 }
}


方法1:

package threadexxcommunication;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Print {
 public boolean flag = false;
 
 public synchronized void print1(){
  for(int i=0;i<=25;i++){
   while(flag){
    try {
     wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   System.out.print(2*i+1 +""+ (2*i+2));
   flag = true;
   notifyAll();
  }
  
 }
 public synchronized void print2(){
  for(char c='A';c<='Z';c++){
   while(!flag){
    try {
     wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   System.out.print(c);
   flag = false;
   notifyAll();
  }
  
 }
 public static void main(String[] args) {
  ExecutorService pool = Executors.newFixedThreadPool(2);
  Print p = new Print();
  Thread target1 = new Thread() {
   @Override
   public void run() {
    p.print1();
   }
  };
  Thread target2 = new Thread() {
   @Override
   public void run() {
    p.print2();
   }
  };
  pool.submit(target1);
  pool.submit(target2);
  pool.shutdown();
 }
}

方法2

package threadexxcommunication;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PrintNew {
 public boolean flag = false;
 public int count =0;
 public synchronized void printNum(String outputString) throws InterruptedException{
  while(flag){
   wait();
  }
  System.out.print(outputString);
  count ++;
  if((count+1) % 3 ==0){
   flag=true;
   notifyAll();
  }
 }
 public synchronized void printChar(String outputString) throws InterruptedException{
  while(!flag){
   wait();
  }
  System.out.print(outputString);
  count++;
  flag =false;
  notifyAll();
 }
 public static void main(String[] args) {
  ExecutorService pool = Executors.newFixedThreadPool(2);
  PrintNew p = new PrintNew();
  Runnable task1 = new Runnable() {
   @Override
   public void run() {
    for(int i=1;i<=52;i++){
     try {
      p.printNum(i+"");
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
    
   }
  };
  Runnable task2 = new Runnable() {
   @Override
   public void run() {
    for(char c ='A';c<='Z';c++){
     try {
      p.printChar(c+"");
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
    
   }
  };
  pool.submit(task1);
  pool.submit(task2);
  pool.shutdown();
 }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值