三线程打印ABC的问题
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。为啥我这么写结果不对呢? 线程 thread --------------------编程问答-------------------- 这道题网上有。你网上搜索下吧。 --------------------编程问答-------------------- 写了一个:
--------------------编程问答-------------------- 方法有很多种,这算是其中一种吧。网上看到的
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();
}
}
二楼的方法我感觉会产生死锁。
即两个线程占有不同资源,都不释放,导致无法仅需运行。。
楼主自己往下推理下试试。
--------------------编程问答--------------------
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();
}
}
仔细看了一下,楼主这里
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();
}
}
}
补充:Java , Java相关