Thread suspend()挂起resume()恢复
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; public class ThreadVSR extends JPanel implements Runnable{
private static final String[] symbolList={"|","/","--","\\","/","--","\\"};
private Thread runThread;
private JTextField symbolTF; public ThreadVSR() {
symbolTF=new JTextField();
symbolTF.setEditable(false);
symbolTF.setFont(new Font("Monospaced",Font.BOLD,26));
symbolTF.setHorizontalAlignment(JTextField.CENTER);
final JButton suspendB=new JButton("suspend");
final JButton resumeB=new JButton("resume");
suspendB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
suspendNow();
}
});
resumeB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resumeNow();
}
});
JPanel in=new JPanel();
in.setLayout(new GridLayout(0,1,3,3));
in.add(symbolTF);
in.add(suspendB);
in.add(resumeB);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(in);
}
private void suspendNow(){
if (this.runThread!=null){
runThread.suspend();
}
}
private void resumeNow(){
if (this.runThread!=null){
this.runThread.resume();
}
} @Override
public void run() {
try {
runThread=Thread.currentThread();
int count=0;
while (true){
symbolTF.setText(symbolList[count%symbolList.length]);
Thread.sleep(200);
count++;
System.out.println(count);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
runThread=null;
}
public static void main(String[] args){
ThreadVSR vrs=new ThreadVSR();
Thread t=new Thread(vrs);
t.start();
JFrame f=new JFrame("suspend-resume-");
f.setContentPane(vrs);
f.setSize(320,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Thread suspend()挂起resume()恢复的更多相关文章
- JAVA多线程之线程的挂起与恢复(suspend方法与resume方法)
一,介绍 本文讨论JAVA多线程中,使用 thread.suspend()方法暂停线程,使用 thread.resume()恢复暂停的线程 的特点. 先介绍二个关于线程的基本知识: ①线程的执行体是r ...
- java 多线程7: (suspend方法与resume方法) 挂起与恢复
suspend方法与resume 是 实例方法,已废弃 缺点一:会造成独占,从而造成死锁 缺点二:会造成数据不同步,不能保证数据原子性
- Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated ?
Thread.stop, Thread.suspend, Thread.resume被标记为废弃的方法.在查看JDK的文档时,提到了下面的参考文章,先是英文版,接着是中文翻译. Why is Thre ...
- 被废弃的 Thread.stop, Thread.suspend, Thread.resume 和Runtime.runFinalizersOnExit
最近学习多线程的知识,看到API里说这些方法被废弃了,就查了一下原因 Thread.stop 这个方法会解除被加锁的对象的锁,因而可能造成这些对象处于不一致的状态,而且这个方法造成的ThreadDea ...
- java中的线程(1):如何正确停止线程Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?
转自 : http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html 1.Why is Th ...
- 转: 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17095733 挂起和恢复线程 Thread 的API中包含两个被淘汰的方法,它们用 ...
- Java知多少(65)线程的挂起、恢复和终止
有时,线程的挂起是很有用的.例如,一个独立的线程可以用来显示当日的时间.如果用户不希望用时钟,线程被挂起.在任何情形下,挂起线程是很简单的,一旦挂起,重新启动线程也是一件简单的事. 挂起,终止和恢复线 ...
- 【Java并发编程】:线程挂起、恢复与终止
挂起和恢复线程 Thread 的API中包含两个被淘汰的方法,它们用于临时挂起和重启某个线程,这些方法已经被淘汰,因为它们是不安全的,不稳定的.如果在不合适的时候挂起线程(比如,锁定共享资源时 ...
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法
挂起和恢复线程 Thread 的API中包含两个被淘汰的方法,它们用于临时挂起和重启某个线程,这些方法已经被淘汰,因为它们是不安全的,不稳定的.如果在不合适的时候挂起线程(比如,锁定共享资源时), ...
随机推荐
- 【洛谷P1090】合并果子
合并果子 题目链接 贪心:每次先合并最小的两堆果子 用堆实现 #include<iostream> #include<cstdio> using namespace std; ...
- url 解析
最近在做一个单页应用,使用AngularJS来处理一些页内路由(哈希#后的路由变化).自然会要解析URL中的参数.使用AngularJS自带的方法$location.search();可以自动将参数整 ...
- rcnn spp_net hcp
rcnn开创性工作,但是计算时间太长,重复计算太大. spp_net将重复计算避免了. hcp是yan shuicheng那边的,是用bing生成regions,然后用normalized cut将这 ...
- axios 二进制流导出
axios 二进制流导出 axios({ url: 'http://xxx', method:'get', data:{}, headers:{ 'ContentType': 'application ...
- Oracle AWR快照管理与常见问题
1.手动创建Snapshots exec dbms_workload_repository.create_snapshot(); OR BEGIN DBMS_WORKLOAD_REPOSITORY.C ...
- angular学习之angular-phonecat项目的实现
---恢复内容开始--- AngularJS官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些Android手机,了解它们的详细信息,并进行搜索和排序操作. ...
- oracle之bitmap索引
oracle常见的索引是BTree索引和Bitmap索引. BTree索引特点: 默认索引 适合大量增删改查 不能用or操作符 适合高基数的列(即唯一值多) 创建sql:create index li ...
- ethereum(以太坊)(八)--Address
pragma solidity ^0.4.0; contract Test{ address _owner; uint160 _c; constructor() public{ _owner = 0x ...
- is和==,encode和decode
0.编码解码 >encode和decode a = "你好" s = a.encode("GBK") print(s) # b'\xc4\xe3\xba\ ...
- The Road to learn React书籍学习笔记(第一章)
react灵活的生态圈 Small Application Boilerplate: create-react-app Utility: JavaScript ES6 and beyond Styli ...