解决方案 »
- 多个用户操作项目,页面崩溃!!!求解决
- 帮我看看是怎么回事?数据库连接问题?
- 我告诉大家啊,不是我不给大分,只不过我给的时候不知道怎么平均给,所以有的人就没有,有的人就有,具体我只能说不好意思了,谢谢大家
- 原格式处理不正确的怎么办?
- 下载空间更新了spring入门学习笔记.欢迎大家下载
- 关于读取*.properts文件
- jsp 访问数据库问题
- jvm 路径迷惑,请大家帮忙理解
- 问高手一个很简单的问题----------如果去掉最后一个字符后面的逗号????
- 在用户注册例子中,想把一些已存入数据库的用户删除,可是删除不了,我遇到了麻烦,请支援!!!
- 程序大赛最后一题。帮我做出来送分+磕响头
- 请教一个大家经常在聊天时接触的知识
sleep(1000);
}
e.start();
e线程wait
判断前四个线程的标示如果都true则notify
public class JoinThread extends Thread { public static int n = 0; static synchronized void inc() {
n++;
} public void run() {
for (int i = 0; i < 10; i++)
try {
inc();
sleep(3); // 为了使运行结果更随机,延迟3毫秒
} catch (Exception e) { }
} public static void main(String[] args) throws Exception {
Thread threads[] = new Thread[100];
for (int i = 0; i < threads.length; i++)
// 建立100个线程
threads[i] = new JoinThread();
for (int i = 0; i < threads.length; i++)
// 运行刚才建立的100个线程
threads[i].start();
if (args.length > 0)
for (int i = 0; i < threads.length; i++)
// 100个线程都执行完后继续
threads[i].join();
System.out.println("n=" + JoinThread.n);
}}
public class TestThread {
private boolean falg = false;
public TestThread() {
}
public void task1() {
synchronized (this) {
while (!falg) {
try {
System.out.println("wait ....");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("I'm E");
}
}
public void task2() {
synchronized (this) {
System.out.println("Do other");
falg=true;
this.notifyAll();
}
}
}
public class ThreadDemo1 extends Thread {
private TestThread tt;
public ThreadDemo1(TestThread tt){
this.tt=tt;
}
public void run() {
tt.task1();
}
}
public class ThreadDemo2 extends Thread {
private TestThread tt;
public ThreadDemo2(TestThread tt){
this.tt=tt;
}
public void run() {
tt.task2();
}
}
public class Test {
public static void main(String[] args){
TestThread tt=new TestThread();
new ThreadDemo1(tt).start();
new ThreadDemo2(tt).start();
}
}