假设main启动了3个A线程,3个B线程现在希望 这6个线程执行完后,然后main方法在接下去执行,有什么简单的方法没有自己实现了一个,感觉很繁琐
package threadTest;public class TestThread { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub Counter counter = new Counter();
Thread t1 = new Thread(new Adder(counter));
Thread t2 = new Thread(new Adder(counter));
Thread t3 = new Thread(new Adder(counter)); Thread t4 = new Thread(new Subtracter(counter));
Thread t5 = new Thread(new Subtracter(counter));
Thread t6 = new Thread(new Subtracter(counter)); t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start(); // CyclicBarrier cb = new CyclicBarrier(6); synchronized (counter) {
while (Counter.getCondition() < 6) {
// System.out.println(Counter.getCondition());
counter.wait();
}
} System.out.println(Counter.getId());
System.out.println("main end"); }}class Adder implements Runnable { private Object o; public Adder(Object o) {
this.o = o;
} @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10000; i++)
Counter.addId();
synchronized (o) {
Counter.addCondition();
System.out.println(Thread.currentThread().getName() + " end");
if (Counter.getCondition() == 6)
o.notify();
} }}class Subtracter implements Runnable { private Object o; public Subtracter(Object o) {
this.o = o;
} @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++)
Counter.subtractId(); synchronized (o) {
Counter.addCondition();
System.out.println(Thread.currentThread().getName() + " end");
if (Counter.getCondition() == 6)
o.notify();
} }}class Counter {
private static int id = 1000;
private static int condition = 0; public synchronized static int getCondition() {
return condition;
} public synchronized static void addCondition() {
condition++;
System.out.println("condition=" + condition);
} public static void addId() {
id++;
} public static void subtractId() {
id--;
} public static int getId() {
return id;
} public static void setId(int id) {
Counter.id = id;
}}
package threadTest;public class TestThread { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub Counter counter = new Counter();
Thread t1 = new Thread(new Adder(counter));
Thread t2 = new Thread(new Adder(counter));
Thread t3 = new Thread(new Adder(counter)); Thread t4 = new Thread(new Subtracter(counter));
Thread t5 = new Thread(new Subtracter(counter));
Thread t6 = new Thread(new Subtracter(counter)); t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start(); // CyclicBarrier cb = new CyclicBarrier(6); synchronized (counter) {
while (Counter.getCondition() < 6) {
// System.out.println(Counter.getCondition());
counter.wait();
}
} System.out.println(Counter.getId());
System.out.println("main end"); }}class Adder implements Runnable { private Object o; public Adder(Object o) {
this.o = o;
} @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10000; i++)
Counter.addId();
synchronized (o) {
Counter.addCondition();
System.out.println(Thread.currentThread().getName() + " end");
if (Counter.getCondition() == 6)
o.notify();
} }}class Subtracter implements Runnable { private Object o; public Subtracter(Object o) {
this.o = o;
} @Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++)
Counter.subtractId(); synchronized (o) {
Counter.addCondition();
System.out.println(Thread.currentThread().getName() + " end");
if (Counter.getCondition() == 6)
o.notify();
} }}class Counter {
private static int id = 1000;
private static int condition = 0; public synchronized static int getCondition() {
return condition;
} public synchronized static void addCondition() {
condition++;
System.out.println("condition=" + condition);
} public static void addId() {
id++;
} public static void subtractId() {
id--;
} public static int getId() {
return id;
} public static void setId(int id) {
Counter.id = id;
}}
解决方案 »
- 200分 使用CNNIC查询IP归属地信息
- 截取字符串中特定字符的问题
- 怎样在线程池中开始和停止一个线程
- 请教Java 的Reflection机制的问题
- 在GDI+中如何指定并删除一个图元?谢,分不够再加。急,谢谢大伙。
- WINDOWS XP的环境变量在哪里设置?
- 拜求一个统计的SQL
- JBuilder5不支持此程序,不知道什么原因,请各位哥哥姐姐多多指教,不胜感激!!!
- Sun ONE Studio有什么用
- 执行时出错:Exception in thread "main" java.lang.NoClassDefFoundError: file
- StringBuffer.capacity()返回的是什么?
- java 使用registry-3.1.3读取注册表字符串中文的乱码问题? 真心求助!!!急求!!正确的,分全给!
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0; if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}