【Java并发】1.8暂停线程
暂停线程意味着此线程可以恢复运行。在Java多线程中用suspend()方法暂停线程,使用resume()方法恢复线程的执行。
如何使用suspend与resume方法?
public class MyThread extends Thread {
private long i = 1;
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(5000);
//A段
thread.suspend();
System.out.println("A="+System.currentTimeMillis()+",i="+thread.getI());
Thread.sleep(5000);
System.out.println("A="+System.currentTimeMillis()+",i="+thread.getI());
//B段
thread.resume();
Thread.sleep(5000);
//C段
thread.suspend();
System.out.println("B="+System.currentTimeMillis()+",i="+thread.getI());
Thread.sleep(5000);
System.out.println("B="+System.currentTimeMillis()+",i="+thread.getI());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public long getI() {
return i;
}
public void setI(long i) {
this.i = i;
}
@Override
public void run() {
super.run();
while (true) {
i++;
}
}
}
A=1513740411248,i=2343457221
A=1513740416249,i=2343457221
B=1513740421249,i=5066938112
B=1513740426249,i=5066938112
根据程序运行结果,发现线程可以被暂停以及恢复。
suspend与resume方法的缺点——独占
public class SynchronizedObject {
public static void main(String[] args) {
try {
final SynchronizedObject object = new SynchronizedObject();
Thread threadA = new Thread() {
@Override
public void run() {
super.run();
object.printString();
}
};
threadA.setName("a");
threadA.start();
Thread.sleep(1000);
Thread threadB = new Thread() {
@Override
public void run() {
super.run();
System.out.println("threadB启动了,但是进不了printString()方法!只打印出一个begin");
System.out.println("因为printString()方法被a线程锁定并且永久suspend暂停了!");
object.printString();
}
};
threadB.setName("b");
threadB.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public void printString() {
System.out.println("begin.");
if (Thread.currentThread().getName().equals("a")) {
System.out.println("a线程被永久suspend了~");
Thread.currentThread().suspend();
}
System.out.println("end.");
}
}
begin.
a线程被永久suspend了~
threadB启动了,但是进不了printString()方法!只打印出一个begin
因为printString()方法被a线程锁定并且永久suspend暂停了!
还有另一种独占锁的情况也需注意:
public class MyThread2 extends Thread{
private long i = 1;
public long getI() {
return i;
}
public void setI(long i) {
this.i = i;
}
@Override
public void run() {
super.run();
while (true) {
i++;
//System.out.println("i="+i);
}
}
public static void main(String[] args) {
try {
MyThread2 thread = new MyThread2();
thread.start();
Thread.sleep(1000);
thread.suspend();
System.out.println("main end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
main end
程序运行结果为main end .
但是如果把System.out.println("i="+i); 注释去掉,程序运行结果为:
......
i=183214
i=183215
i=183216
i=183217
i=183218
i=183219
i=183220
i=183221
此时main end 未输出,是由于运行到println方法内部时,同步锁未被释放。
println内部源码如下:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
这导致当前PringStream对象的println方法一直呈现“暂停”状态,并且锁未被释放,而main方法中的代码System.out.println("main end");迟迟不能打印。
虽然suspend()方法已过期作废,但是研究其过期的原因还是非常有意义的。
suspend与resume缺点——不同步
在使用suspend与resume方法时也会出现线程暂停导致的数据不同步问题。
public class MyObject{
private String name = "name";
private String pwd = "pwd";
public void setValue(String name,String pwd){
this.name=name;
if(Thread.currentThread().getName().equals("a")){
System.out.println("停止a线程!");
Thread.currentThread().suspend();
}
this.pwd=pwd;
}
public void printString(){
System.out.println("name:" + name + ",pwd:" + pwd);
}
public static void main(String[] args) throws InterruptedException {
final MyObject object = new MyObject();
Thread thread1 = new Thread(){
@Override
public void run() {
super.run();
object.setValue("root","123456");
}
};
thread1.setName("a");
thread1.start();
Thread.sleep(1000);
Thread thread2 = new Thread(){
@Override
public void run() {
super.run();
object.printString();
}
};
thread2.start();javascript:void(null)
}
}
停止a线程!
name:root,pwd:pwd
程序出现值不同步的情况。使用suspend需特别注意。
如何解决
未完待续...