首页 > 代码库 > 字符串交替打印 操作方法
字符串交替打印 操作方法
1 package cn.itcast.servlet; 2 3 import java.util.concurrent.LinkedBlockingQueue; 4 import java.util.concurrent.locks.Condition; 5 import java.util.concurrent.locks.Lock; 6 import java.util.concurrent.locks.ReentrantLock; 7 8 9 interface StringTurn 10 { 11 public void print1(); 12 public void print2(); 13 14 } 15 /** 16 * 交替打印字符串 17 */ 18 class StringTurnPrint implements StringTurn 19 { 20 private int i=0; 21 private String str="i want to fly"; 22 private Object mutex=new Object(); 23 private boolean flag=true; 24 public void print1() 25 { 26 synchronized(mutex) 27 { 28 while(i<str.length()) 29 { 30 if(flag==false) 31 { 32 try { 33 mutex.wait(); 34 } catch (InterruptedException e) { 35 e.printStackTrace(); 36 } 37 } 38 flag=false; 39 if(i>=str.length()) 40 return; 41 System.out.println(Thread.currentThread().getName() +" print "+ str.charAt(i)); 42 i++; 43 mutex.notify(); 44 } 45 } 46 } 47 public void print2() 48 { 49 synchronized(mutex) 50 { 51 while(i<str.length()) 52 { 53 if(flag==true) 54 { 55 try { 56 mutex.wait(); 57 } catch (InterruptedException e) { 58 e.printStackTrace(); 59 } 60 } 61 flag=true; 62 if(i>=str.length()) 63 return; 64 System.out.println(Thread.currentThread().getName() +" print "+ str.charAt(i)); 65 i++; 66 mutex.notify(); 67 } 68 } 69 } 70 } 71 72 class StringTurnPrint2 implements StringTurn 73 { 74 Lock lock=new ReentrantLock(); 75 boolean flag=true; 76 private String str="i want to fly"; 77 int i=0; 78 Condition con1=lock.newCondition(); 79 Condition con2=lock.newCondition(); 80 81 public void print1() 82 { 83 lock.lock(); 84 while(i<str.length()) 85 { 86 if(flag==false) 87 { 88 try { 89 con1.await(); 90 } catch (InterruptedException e) { 91 e.printStackTrace(); 92 } 93 } 94 flag=false; 95 if(i>=str.length()) 96 return; 97 System.out.println(Thread.currentThread().getName() +" print "+ str.charAt(i)); 98 i++; 99 con2.signal();100 }101 lock.unlock();102 }103 104 public void print2()105 {106 lock.lock();107 while(i<str.length())108 {109 if(flag==true)110 {111 try {112 con2.await();113 } catch (InterruptedException e) {114 e.printStackTrace();115 }116 }117 flag=true;118 if(i>=str.length())119 return;120 System.out.println(Thread.currentThread().getName() +" print "+ str.charAt(i));121 i++;122 con1.signal();123 }124 lock.unlock();125 }126 }127 128 class Thread1 extends Thread129 {130 StringTurn stringTurnPrint;131 Thread1(StringTurn s)132 {133 stringTurnPrint=s;134 this.setName("thread1");135 }136 @Override137 public void run() {138 this.stringTurnPrint.print1();139 }140 }141 class Thread2 extends Thread142 {143 StringTurn stringTurnPrint;144 Thread2(StringTurn s)145 {146 stringTurnPrint=s;147 this.setName("thread2");148 }149 @Override150 public void run() {151 this.stringTurnPrint.print2();152 }153 }154 public class ThreadMain {155 public static void main(String[] args) {156 StringTurn s=new StringTurnPrint();157 StringTurn s2=new StringTurnPrint2();158 Thread1 t1=new Thread1(s2);159 Thread2 t2=new Thread2(s2);160 t1.start();161 t2.start();162 163 164 // 建立一个阻塞队列165 }166 }
管道通信
1 package cn.itcast.servlet; 2 3 import java.io.IOException; 4 import java.io.PipedInputStream; 5 import java.io.PipedOutputStream; 6 7 class StringTurnPrint 8 { 9 String str="i want to fly"; 10 int i=-1; 11 public Character getChar() 12 { 13 i++; 14 if(i>=str.length()) 15 return (Character) null; 16 char ch=str.charAt(i); 17 return ch; 18 } 19 } 20 21 class Thread1 extends Thread 22 { 23 PipedInputStream pipedInputStream; 24 PipedOutputStream pipedOutputStream; 25 StringTurnPrint stringTurnPrint; 26 public Thread1(StringTurnPrint stringTurnPrint,PipedInputStream pipedInputStream,PipedOutputStream pipedOutputStream) { 27 this.stringTurnPrint=stringTurnPrint; 28 this.pipedInputStream=pipedInputStream; 29 this.pipedOutputStream=pipedOutputStream; 30 this.setName("thread1"); 31 } 32 @Override 33 public void run() { 34 try { 35 while(true) 36 { 37 Character ch=stringTurnPrint.getChar(); 38 if(ch==null) 39 { 40 pipedInputStream.close(); 41 pipedOutputStream.close(); 42 return ; 43 } 44 pipedOutputStream.write(ch); 45 pipedOutputStream.flush(); 46 int r=pipedInputStream.read(); 47 System.out.println(Thread.currentThread().getName() + " print :"+(char)r); 48 } 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 class Thread2 extends Thread 55 { 56 PipedInputStream pipedInputStream; 57 PipedOutputStream pipedOutputStream; 58 StringTurnPrint stringTurnPrint; 59 public Thread2( StringTurnPrint stringTurnPrint,PipedInputStream pipedInputStream,PipedOutputStream pipedOutputStream) { 60 this.stringTurnPrint=stringTurnPrint; 61 this.pipedInputStream=pipedInputStream; 62 this.pipedOutputStream=pipedOutputStream; 63 this.setName("thread2"); 64 } 65 @Override 66 public void run() { 67 try { 68 while(true) 69 { 70 71 72 int r=pipedInputStream.read(); 73 System.out.println(Thread.currentThread().getName() + " print :"+(char)r); 74 75 Character ch=stringTurnPrint.getChar(); 76 if(ch==null) 77 { 78 pipedInputStream.close(); 79 pipedOutputStream.close(); 80 return ; 81 } 82 pipedOutputStream.write(ch); 83 pipedOutputStream.flush(); 84 85 } 86 } catch (IOException e) { 87 e.printStackTrace(); 88 } 89 } 90 } 91 public class ThreadPiped { 92 93 public static void main(String[] args) throws IOException { 94 PipedInputStream pipedInputStream=new PipedInputStream(); 95 PipedOutputStream pipedOutputStream=new PipedOutputStream(); 96 PipedInputStream pipedInputStream2=new PipedInputStream(); 97 PipedOutputStream pipedOutputStream2=new PipedOutputStream(); 98 99 pipedInputStream.connect(pipedOutputStream2);100 pipedOutputStream.connect(pipedInputStream2);101 102 StringTurnPrint stringTurnPrint=new StringTurnPrint();103 new Thread1(stringTurnPrint, pipedInputStream, pipedOutputStream).start();104 new Thread2(stringTurnPrint, pipedInputStream2, pipedOutputStream2).start();105 106 107 }108 109 }
字符串交替打印 操作方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。