三线程打印ABC的问题

wolfwestern 2013-10-29 11:12:00
public class Print {
public static void main(String[] args) {
String aString=new String();
String bString=new String();
String cString=new String();
PrintStr pStr1=new PrintStr("A", cString,bString,10);
PrintStr pStr2=new PrintStr("B", aString,cString,10);
PrintStr pStr3=new PrintStr("C", bString,aString,10);
new Thread(pStr1).start();
new Thread(pStr2).start();
new Thread(pStr3).start();
}

}

class PrintStr implements Runnable
{
int num;
String string;
String string1;
String string2;
public PrintStr(String string, String string1, String string2, int num) {
// TODO Auto-generated constructor stub
this.num=num;
this.string=string;
this.string1=string1;
this.string2=string2;
}
public void run() {
synchronized (string1) {
synchronized (string2) {
while (num>0) {
System.out.println(string);
num--;
try {
string1.notify();
string2.wait();

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//string1.notify();
}

}

}
}
建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC。为啥我这么写结果不对呢?
...全文
570 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
失落夏天 2013-10-30
  • 打赏
  • 举报
回复
引用 楼主 wolfwestern 的回复:
public class Print { public static void main(String[] args) { String aString=new String(); String bString=new String(); String cString=new String(); PrintStr pStr1=new PrintStr("A", cString,bString,10); PrintStr pStr2=new PrintStr("B", aString,cString,10); PrintStr pStr3=new PrintStr("C", bString,aString,10); new Thread(pStr1).start(); new Thread(pStr2).start(); new Thread(pStr3).start(); } } class PrintStr implements Runnable { int num; String string; String string1; String string2; public PrintStr(String string, String string1, String string2, int num) { // TODO Auto-generated constructor stub this.num=num; this.string=string; this.string1=string1; this.string2=string2; } public void run() { synchronized (string1) { synchronized (string2) { while (num>0) { System.out.println(string); num--; try { string1.notify(); string2.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //string1.notify(); } } } } 建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC。为啥我这么写结果不对呢?
仔细看了一下,楼主这里 public void run() { synchronized (string1) {//这里对string1加锁。 synchronized (string2) {//这里对string1加锁。 while (num > 0) { System.out.println(string); num--; try { string1.notify(); string2.wait();//这里string2等待,这个线程里面由于c,b同时被锁。 另外一个线程里面需要资源ac而无法获取,造成死锁 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // string1.notify(); } } }
失落夏天 2013-10-30
  • 打赏
  • 举报
回复
方法有很多种,这算是其中一种吧。网上看到的 二楼的方法我感觉会产生死锁。 即两个线程占有不同资源,都不释放,导致无法仅需运行。。 楼主自己往下推理下试试。

public class Test extends Thread {
	private static String currentThread = "A";
	private static byte[] lock = new byte[0];
	private String name = "";
	private int count = 10;

	public Test(String name) {
		this.name = name;
	}

	public void run() {
		while (count > 0) {
			synchronized (lock) {
				if (currentThread.equals(this.name)) {
					System.out.println(name);
					count--;
					if (currentThread.equals("A")) {
						currentThread = "B";
					} else if (currentThread.equals("B")) {
						currentThread = "C";
					} else if (currentThread.equals("C")) {
						currentThread = "A";
						System.out.println();
					}
				}
			}
		}
	}

	/** * @param args */
	public static void main(String[] args) {
		(new Test("A")).start();
		(new Test("B")).start();
		(new Test("C")).start();
	}
}
teemai 2013-10-30
  • 打赏
  • 举报
回复
写了一个:

import java.util.concurrent.Semaphore;  

public class ThreadSync {  
    static class ConditionThread extends Thread {  
        ConditionThread(Semaphore preCond, Semaphore postCond, String text) {  
            this.preCond = preCond;  
            this.postCond = postCond;  
            this.text = text;  
        }  
  
        private Semaphore preCond;  
        private Semaphore postCond;  
        private String text;  
  
        @Override  
        public void run() {  
            for (int i = 0; i < 10; i++) {  
                try {  
                    preCond.acquire();  
                    System.out.print(text);  
                    postCond.release();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
  
    public static void main(String[] args) throws InterruptedException {  
        Semaphore semaphoreA = new Semaphore(0);  
        Semaphore semaphoreB = new Semaphore(0);  
        Semaphore semaphoreC = new Semaphore(1);  
  
        Thread threadA = new ConditionThread(semaphoreC, semaphoreA, "A");  
        Thread threadB = new ConditionThread(semaphoreA, semaphoreB, "B");  
        Thread threadC = new ConditionThread(semaphoreB, semaphoreC, "C");  
  
        threadA.start();  
        threadB.start();  
        threadC.start();  
  
        threadA.join();  
        threadB.join();  
        threadC.join();  
    }  
}  

tony4geek 2013-10-30
  • 打赏
  • 举报
回复
这道题网上有。你网上搜索下吧。

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧