暂停线程可以用suspend()
恢复线程可以用resume()
但是这两个方法都已经过期作废
正常使用suspend和resume
package com.myThread;
public class Thread1 extends Thread {
long i = 0;
public Thread1() {
}
public long getI() {
return i;
}
public void setI(long i) {
this.i = i;
}
@Override
public void run() {
super.run();
while (true) {
i++;
}
}
}
package com.test;
import com.myThread.Thread1;
public class Test1 {
public static void main(String[] args) throws InterruptedException {
// 测试1
Thread1 thread1 = new Thread1();
thread1.start();
Thread.sleep(500);
thread1.suspend();//暂停
System.out.println(System.currentTimeMillis()+":a:"+thread1.getI());
Thread.sleep(100);
System.out.println(System.currentTimeMillis()+":a:"+thread1.getI());//观察时间和i的变化
thread1.resume();//恢复
Thread.sleep(500);
thread1.suspend();//暂停
System.out.println(System.currentTimeMillis()+":b:"+thread1.getI());
Thread.sleep(100);
System.out.println(System.currentTimeMillis()+":b:"+thread1.getI());//观察时间和i的变化
thread1.stop();
}
}
测试1打印结果
1452594207178:a:276619864
1452594207278:a:276619864
1452594207778:b:513899984
1452594207878:b:513899984
suspend和resume问题1-独占
使用这个组合,使用不好就会照成公共的同步对象独占,无法释放资源,其他线程则获取不到
这里展示一个比较特殊的情况,println()和suspend一起使用
package com.myThread;
public class Thread2 extends Thread {
long i = 0;
@Override
public void run() {
super.run();
while (true) {
i++;
System.out.println(i);
}
}
}
package com.test;
import com.myThread.Thread2;
public class Test2 {
public static void main(String[] args) throws InterruptedException {
// 测试1
Thread2 thread2 = new Thread2();
thread2.start();
Thread.sleep(500);
thread2.suspend();// 暂停
System.out.println("main end");
}
}
打印结果
100060
100061
100062
100063
100064
100065
分析:
thread2暂停了可是main没有暂停,所以最后应该输出“main end”
可是却没有输出
先来看println()的源代码
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
synchronized (this)是一个同步锁,而这个锁正在被thread2占用
suspend和resume问题2-不同步
package com.myObject;
public class Object3 {
private String name = "c";
private String password = "cc";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
synchronized public void evalute(String name, String password) {
this.name = name;
if ("a".equals(Thread.currentThread().getName())) {
System.out.println("Thread a 暂停");
Thread.currentThread().suspend();
}
this.password = password;
}
@Override
public String toString() {
return "Object5 [name=" + name + ", password=" + password + "]";
}
}
package com.test;
import com.myObject.Object3;
public class Test3 {
public static void main(String[] args) throws InterruptedException {
// 测试1
final Object3 object3 = new Object3();
Thread threada = new Thread(){
@Override
public void run() {
super.run();
object3.evalute("a", "aa");
}
};
threada.setName("a");
threada.start();//会在evalute方法中暂停
Thread.sleep(5000);
Thread threadb = new Thread(){
@Override
public void run() {
super.run();
System.out.println(object3.toString());
}
};
threadb.start();
}
}
测试1打印结果
Thread a 暂停
Object5 [name=a, password=cc]
本文探讨了线程的暂停和恢复操作,通过实例展示了suspend()和resume()方法的用法及其潜在问题,包括资源独占和同步冲突。文章还深入分析了这些操作在不同场景下的表现和影响。
934

被折叠的 条评论
为什么被折叠?



