解决方案 »
- 属性否决有些问题
- 关于executeQuery的问题请高手教我
- 小弟刚来,想知道学java用什么开发环境好,或说当前企业多用什么?(分多多派,来了就有)
- 开始玩blog了,大家看看啊,up有分
- 想学java我要用什么环境呀。提供下载地最好了。给分
- 如何知到SCROLL_SENSITIVEresultset型的ResultSet.和,ResultSet.TYPE_SCROLL_INSENSITIVE 型的ResultSet的行数
- 关于bean的实例化的问题
- 请教两个问题!1. 有没有类似vc中MessageBox()的东西?2.*****************(谢谢)!
- 开发一个java的网络管理程序,高手指点
- 大家在查 java 的某个方法或者类 查什么文档
- Java书上题例,一个小疑问~
- 关于异常的一段代码,为什么不能正常运行
public class Test {
public static void main(String[] args) {
final Thread t1 = new Thread() {
public void run() {
int i = 0;
while(!Thread.interrupted()) {
System.out.println(++ i);
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
break;
}
}
}
};
Thread t2 = new Thread() {
public void run() {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt(); //中断t1
}
};
t1.start();
t2.start();
}}
public class Test {
public static void main(String[] args) {
final Semaphore available = new Semaphore(1);
final Thread t1 = new Thread() {
public void run() {
int i = 0;
while(true) {
try {
available.acquire();
System.out.println(++ i);
Thread.sleep(500L);
available.release();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
};
Thread t2 = new Thread() {
public void run() {
try {
Thread.sleep(1000L);
available.acquire();
System.out.println("暂停");
Thread.sleep(5000L);
available.release();
System.out.println("开始");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
t2.start();
}}
public static void main(String[] args) {
TaskA t1 = new TaskA();
TaskB t2 = new TaskB(t1);
t1.start();
t2.start();
}}class TaskA extends Thread {
int i;
boolean stoped; public void run() {
while(true) {
if(stoped) {
synchronized(this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
doTask();
}
}
void doTask() {
if(i < 100) {
System.out.println(++ i);
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void zanting() {
stoped = true;
}
synchronized void jixu() {
stoped = false;
notifyAll();
}
}class TaskB extends Thread {
TaskA taskA;
TaskB(TaskA taskA) {
this.taskA = taskA;
}
public void run() {
try {
Thread.sleep(1000L);
taskA.zanting();
System.out.println("暂停");
for (char i = 'a'; i < 'f'; i++) {
System.out.println(i);
Thread.sleep(500L);
}
taskA.jixu();
System.out.println("开始");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
也不知道到底能否实现。
private boolean suspend = false;
private String control = ""; // 只是需要一个对象而已,这个对象没有实际意义
public void setSuspend(boolean suspend) {
if (!suspend) {
synchronized (control) {
control.notifyAll();
}
}
this.suspend = suspend;
}
public boolean isSuspend() {
return this.suspend;
}
public void run() {
while (true) {
synchronized (control) {
if (suspend) {
try {
control.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
this.runPersonelLogic();
}
}
protected abstract void runPersonelLogic();
public static void main(String[] args) throws Exception {
MyThread myThread = new MyThread() {
protected void runPersonelLogic() {
System.out.println("myThead is running");
}
};
myThread.start();
Thread.sleep(3000);
myThread.setSuspend(true);
System.out.println("myThread has stopped");
Thread.sleep(3000);
myThread.setSuspend(false);
}
}
被控制线程每执行一句代码后,判断一下标记,如果为标记false让线程执行wait()
控制方只要控制标记就能让另一个线程停止,执行notifyAll()并让标记为true就能让另一个线程继续