字符串交替打印|字符串交替打印 操作方法

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 { 11public void print1(); 12public void print2(); 13 14 } 15 /** 16*交替打印字符串 17*/ 18 class StringTurnPrint implements StringTurn 19 { 20private int i=0; 21private String str="i want to fly"; 22private Object mutex=new Object(); 23private boolean flag=true; 24public void print1() 25{ 26synchronized(mutex) 27{ 28while(i=str.length()) 40return; 41System.out.println(Thread.currentThread().getName() +"print "+ str.charAt(i)); 42i++; 43mutex.notify(); 44} 45} 46} 47public void print2() 48{ 49synchronized(mutex) 50{ 51while(i=str.length()) 63return; 64System.out.println(Thread.currentThread().getName() +"print "+ str.charAt(i)); 65i++; 66mutex.notify(); 67} 68} 69} 70 } 71 72 class StringTurnPrint2 implements StringTurn 73 { 74Lock lock=new ReentrantLock(); 75boolean flag=true; 76private String str="i want to fly"; 77int i=0; 78Condition con1=lock.newCondition(); 79Condition con2=lock.newCondition(); 80 81public void print1() 82{ 83lock.lock(); 84while(i=str.length()) 96return; 97System.out.println(Thread.currentThread().getName() +"print "+ str.charAt(i)); 98i++; 99con2.signal(); 100} 101lock.unlock(); 102} 103 104public void print2() 105{ 106lock.lock(); 107while(i=str.length()) 119return; 120System.out.println(Thread.currentThread().getName() +"print "+ str.charAt(i)); 121i++; 122con1.signal(); 123} 124lock.unlock(); 125} 126 } 127 128 class Thread1 extends Thread 129 { 130StringTurn stringTurnPrint; 131Thread1(StringTurn s) 132{ 133stringTurnPrint=s; 134this.setName("thread1"); 135} 136@Override 137public void run() { 138this.stringTurnPrint.print1(); 139} 140 } 141 class Thread2 extends Thread 142 { 143StringTurn stringTurnPrint; 144Thread2(StringTurn s) 145{ 146stringTurnPrint=s; 147this.setName("thread2"); 148} 149@Override 150public void run() { 151this.stringTurnPrint.print2(); 152} 153 } 154 public class ThreadMain { 155public static void main(String[] args) { 156StringTurn s=new StringTurnPrint(); 157StringTurn s2=new StringTurnPrint2(); 158Thread1 t1=new Thread1(s2); 159Thread2 t2=new Thread2(s2); 160t1.start(); 161t2.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 { 9String str="i want to fly"; 10int i=-1; 11public Character getChar() 12{ 13i++; 14if(i>=str.length()) 15return (Character) null; 16char ch=str.charAt(i); 17return ch; 18} 19 } 20 21 class Thread1 extends Thread 22 { 23PipedInputStream pipedInputStream; 24PipedOutputStream pipedOutputStream; 25StringTurnPrint stringTurnPrint; 26public Thread1(StringTurnPrint stringTurnPrint,PipedInputStream pipedInputStream,PipedOutputStream pipedOutputStream) { 27this.stringTurnPrint=stringTurnPrint; 28this.pipedInputStream=pipedInputStream; 29this.pipedOutputStream=pipedOutputStream; 30this.setName("thread1"); 31} 32@Override 33public void run() { 34try { 35while(true) 36{ 37Character ch=stringTurnPrint.getChar(); 38if(ch==null) 39{ 40pipedInputStream.close(); 41pipedOutputStream.close(); 42return ; 43} 44pipedOutputStream.write(ch); 45pipedOutputStream.flush(); 46int r=pipedInputStream.read(); 47System.out.println(Thread.currentThread().getName() + "print:"+(char)r); 48} 49} catch (IOException e) { 50e.printStackTrace(); 51} 52} 53 } 54 class Thread2 extends Thread 55 { 56PipedInputStream pipedInputStream; 57PipedOutputStream pipedOutputStream; 58StringTurnPrint stringTurnPrint; 59public Thread2(StringTurnPrint stringTurnPrint,PipedInputStream pipedInputStream,PipedOutputStream pipedOutputStream) { 60this.stringTurnPrint=stringTurnPrint; 61this.pipedInputStream=pipedInputStream; 62this.pipedOutputStream=pipedOutputStream; 63this.setName("thread2"); 64} 65@Override 66public void run() { 67try { 68while(true) 69{ 70 71 72int r=pipedInputStream.read(); 73System.out.println(Thread.currentThread().getName() + "print:"+(char)r); 74 75Character ch=stringTurnPrint.getChar(); 76if(ch==null) 77{ 78pipedInputStream.close(); 79pipedOutputStream.close(); 80return ; 81} 82pipedOutputStream.write(ch); 83pipedOutputStream.flush(); 84 85} 86} catch (IOException e) { 87e.printStackTrace(); 88} 89} 90 } 91 public class ThreadPiped { 92 93public static void main(String[] args) throws IOException { 94PipedInputStream pipedInputStream=new PipedInputStream(); 95PipedOutputStreampipedOutputStream=new PipedOutputStream(); 96PipedInputStream pipedInputStream2=new PipedInputStream(); 97PipedOutputStreampipedOutputStream2=new PipedOutputStream(); 98 99pipedInputStream.connect(pipedOutputStream2); 100pipedOutputStream.connect(pipedInputStream2); 101 102StringTurnPrint stringTurnPrint=new StringTurnPrint(); 103new Thread1(stringTurnPrint, pipedInputStream, pipedOutputStream).start(); 104new Thread2(stringTurnPrint, pipedInputStream2, pipedOutputStream2).start(); 105 106 107} 108 109 }


    推荐阅读