package com.xuz.csdn.june27;import java.util.concurrent.atomic.AtomicInteger;public class ThreadABC { private Object lock1 = new Object(); private Object lock2 = new Object(); private Object lock3 = new Object();
private AtomicInteger i = new AtomicInteger(0); public void execute() { new Thread() { public void run() { while (true) { if (i.getAndIncrement() < 10) { System.out.println(Thread.currentThread().getName());
那为啥还要用线程。
感觉和调用方法没有区别, join()可以达到和调用方法一样的效果
用线程是题目给出的要求呢
join()我去试试 不过上面的程序死锁是什么原因?
private Object lock = new Object();
private int intNum; public void execute() { new Thread() {
public void run() {
while (true) {
if (intNum < 10) {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} System.out.println(Thread.currentThread().getName() + " print ABC" + intNum);
intNum++;
} else {
break;
}
} }
}.start(); new Thread() {
public void run() {
while (true) {
if (intNum < 10) {
System.out.println(Thread.currentThread().getName()+" print ABC" + intNum);
intNum++;
synchronized (lock) {
lock.notify();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
}
}.start(); } public static void main(String[] argv) {
new ThreadABC().execute();
}}二个线程的。
是按顺序打印线程的name,在这里是A B C罢了
private Object lock1 = new Object();
private Object lock2 = new Object();
private Object lock3 = new Object();
private AtomicInteger i = new AtomicInteger(0); public void execute() { new Thread() {
public void run() {
while (true) {
if (i.getAndIncrement() < 10) {
System.out.println(Thread.currentThread().getName());
synchronized (lock1) {
try {
lock1.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
lock2.notify();
}
} else {
synchronized (lock2) {
lock2.notify();
}
break;
}
} }
}.start(); new Thread() {
public void run() {
while (true) {
if (i.getAndIncrement() < 10) {
System.out.println(Thread.currentThread().getName());
synchronized (lock2) {
try {
lock2.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock3) {
lock3.notify();
}
} else {
synchronized (lock3) {
lock3.notify();
}
break;
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
if (i.getAndIncrement() < 10) {
System.out.println(Thread.currentThread().getName());
synchronized (lock1) {
lock1.notify();
}
synchronized (lock3) {
try {
lock3.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
synchronized (lock1) {
lock1.notify();
}
break;
}
}
}
}.start();
} public static void main(String[] argv) {
new ThreadABC().execute();
}}可以实现功能,但是觉得写得不好。
main函数里aLk.notify();时,打印A的线程有可能还没开始运行
这样就死锁了.
同样,bLk.notify()时,如果打印B的线程还没开始运行,也有可能出现死锁
Thread a = new Thread("A") {
@Override
public void run() {
System.out.println("thread-A");
for (int i = 0; i < 10; i++) {
synchronized (aLk) { try {
aLk.wait();
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println(this.getName() + " " + i);
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (bLk) {
bLk.notify();
}
}
}
}
};