这边做了一个小测试:
实现了runnable接口,在方法中打印count的值:
int count =0; @Override public void run() { while(true){ System.out.println(count++); } }下面将试图唤醒:
public static void main(String[] args) throws InterruptedException { MyThread t = new MyThread(); Thread thread = new Thread(t,"A"); thread.start(); Thread.sleep(10); thread.suspend(); System.out.println("The end"); thread.resume(); }查看控制台:
从控制台可以看出我这边在调用suspend方法之后,程序并没有继续打印,而且也没有打印the end.
很明显线程被锁死了,查看println()方法可以看出这里是一个线程安全的方法,在调用suspend方法之后,对象count被println锁住,线程无法被恢复,而且程序不能继续执行
,所以我们在使用suspend之时很有可能由于某种原因操作线程同步的方法之后可能会造成死锁,这也有可能是suspend,resume被废弃的原因吧.
如若不足或不对之处还望各位指出谢谢!
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
stop也是一个被废弃的方法,因为stop是立即终止线程,这会使我们程序没法保证完整性,因为在我程序还没有执行完毕突然线程结束导致不可控制的错误