题目:有三个线程分别打印A、B、C,请用多线程编程实现,在屏幕上循环打印10次ABCABC…_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 题目:有三个线程分别打印A、B、C,请用多线程编程实现,在屏幕上循环打印10次ABCABC…

题目:有三个线程分别打印A、B、C,请用多线程编程实现,在屏幕上循环打印10次ABCABC…

 2012/2/29 9:21:10  mnbwz  程序员俱乐部  我要评论(0)
  • 摘要:题目:有三个线程分别打印A、B、C,请用多线程编程实现,在屏幕上循环打印10次ABCABC…方法一:使用wait()、notify()控制打印次序publicclassTest{publicstaticObjecta=newObject();publicstaticObjectb=newObject();publicstaticObjectc=newObject();publicclassRunner1implementsRunnable{publicvoidrun(){for(inti=0
  • 标签:实现 多线程 编程 线程

题目:有三个线程分别打印A、B、C,请用多线程编程实现,在屏幕上循环打印10次ABCABC…
方法一:使用wait()、notify()控制打印次序

public class Test {
    public static Object a = new Object();
    public static Object b = new Object();
    public static Object c = new Object();

    public class Runner1 implements Runnable {
		public void run() {
		    for (int i = 0; i < 10; i++) {
				try {
				    synchronized (a) {
						// System.out.println("a is locked by t1");
						synchronized (b) {
						    // System.out.println("b is locked by t1");
						    System.out.print("A");
						    b.notify();
						    // System.out.println("t1 notify b");
						}
						if (i < 9) {
						    a.wait();
						}
				    }
				} catch (InterruptedException e) {
				    e.printStackTrace();
				}
		    }
		}
    }

    public class Runner2 implements Runnable {
		public void run() {
		    for (int i = 0; i < 10; i++) {
				try {
				    synchronized (b) {
						// System.out.println("b is locked by t2");
						synchronized (c) {
						    // System.out.println("c is locked by t2");
						    System.out.print("B");
						    c.notify();
						    // System.out.println("t2 notify c");
						}
						if (i < 9) {
						    b.wait();
						}
				    }
				} catch (InterruptedException e) {
				    e.printStackTrace();
				}
		    }
		}
    }

    public class Runner3 implements Runnable {
		public void run() {
		    for (int i = 0; i < 10; i++) {
				try {
				    synchronized (c) {
						// System.out.println("c is locked by t3");
						synchronized (a) {
						    // System.out.println("a is locked by t3");
						    System.out.print("C");
						    a.notify();
						    // System.out.println("t3 notify a");
						}
						if (i < 9) {
						    c.wait();
						}
				    }
				} catch (InterruptedException e) {
				    e.printStackTrace();
				}
		    }
		}
    }

    public static void main(String[] args) {
		Test t = new Test();
		Thread t1 = new Thread(t.new Runner1(), "t1");
		Thread t2 = new Thread(t.new Runner2(), "t2");
		Thread t3 = new Thread(t.new Runner3(), "t3");
		t1.start();
		try {
		    Thread.sleep(1);
		} catch (InterruptedException e1) {
		    e1.printStackTrace();
		}
		t2.start();
		try {
		    Thread.sleep(1);
		} catch (InterruptedException e) {
		    e.printStackTrace();
		}
		t3.start();
    }
}

?

方法二:使用管道流在进程间传递消息控制打印次序

import java.io.*;
class RunnerA implements Runnable{
	DataInputStream disA=null;
	DataOutputStream dosA=null;
	public RunnerA(PipedInputStream pisA,PipedOutputStream posA){
		disA=new DataInputStream(pisA);
		dosA=new DataOutputStream(posA);
	}
	public void run(){
		try{
			for(int i=0;i<10;i++){
				if(i==0){
					System.out.print("A");
					dosA.writeChar('A');
				}else if(i==9){
					char c=disA.readChar();
					System.out.print("A");
					dosA.writeChar('O');
				}else{
					char c=disA.readChar();
					System.out.print("A");
					dosA.writeChar('A');
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
class RunnerB implements Runnable{
	DataInputStream disB=null;
	DataOutputStream dosB=null;
	public RunnerB(PipedInputStream pisB,PipedOutputStream posB){
		disB=new DataInputStream(pisB);
		dosB=new DataOutputStream(posB);
	}
	public void run(){
		try{
			char c=disB.readChar();
			while(true){
				if(c=='O'){
					System.out.print("B");
					dosB.writeChar('O');
					break;
				}
				if(c=='A'){
					System.out.print("B");
					dosB.writeChar('B');
					c=disB.readChar();
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
class RunnerC implements Runnable{
	DataInputStream disC=null;
	DataOutputStream dosC=null;
	public RunnerC(PipedInputStream pisC,PipedOutputStream posC){
		disC=new DataInputStream(pisC);
		dosC=new DataOutputStream(posC);
	}
	public void run(){
		try{
			char c=disC.readChar();
			while(true){
				if(c=='O'){
					System.out.print("C");
					break;
				}
				if(c=='B'){
					System.out.print("C");
					dosC.writeChar('C');
					c=disC.readChar();
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
public class Test{
	public static void main(String[] args){
		PipedOutputStream posA=new PipedOutputStream();
		PipedInputStream pisA=new PipedInputStream();
		PipedOutputStream posB=new PipedOutputStream();
		PipedInputStream pisB=new PipedInputStream();
		PipedOutputStream posC=new PipedOutputStream();
		PipedInputStream pisC=new PipedInputStream();
		try{
			pisA.connect(posC);
			pisB.connect(posA);
			pisC.connect(posB);
		}catch(IOException e){
			e.printStackTrace();
		}
		
		Thread ta=new Thread(new RunnerA(pisA,posA),"ta");
		Thread tb=new Thread(new RunnerB(pisB,posB),"tb");
		Thread tc=new Thread(new RunnerC(pisC,posC),"tc");
		
		ta.start();
		tb.start();
		tc.start();
	}  
}

?

发表评论
用户名: 匿名