javasuspent的简单介绍
本篇文章给大家谈谈javasuspent,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、JAVA多线程suspend,resume和wait,notify的区别
- 2、java suspend()的问题
- 3、java语句暂停
- 4、java多线程为什么弃用stop和suspend
- 5、java中如何暂停一个程序?
- 6、java程序运行过程中如何暂停,恢复?
JAVA多线程suspend,resume和wait,notify的区别
suspend,使线程进入停滞状态,除非收到resume消息,否则该线程不会变回可执行状态。
wait():使一个线程处于等待状态,并且释放所持有的对象的lock;
sleep():使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方
法要捕捉InterruptedException异常;
notify():唤醒一个处于等待状态的线程,注意的是在调用此方法的时候,
并不能确切的唤醒某一个等待状态的线程,而是由JVM确定唤醒哪个线程,而且
不是按优先级;
notityAll():唤醒所有处入等待状态的线程,注意并不是给所有唤醒线程一
个对象的锁,而是让它们竞争。
java suspend()的问题
suspend()该方法在jdk1.6中已经不推荐使用了,过时了。如果是非阻塞线程只加①句,“使用一个共享变量,线程周期性检测这一变量的值”。如果线程被阻塞,它便不能核查共享变量,也就不能停止。因此不加①只加②即可实现。
class A extends Thread
{
volatile boolean stop = false;
static class B
{
public void du() throws Exception
{
A thread = new A();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Asking thread to stop...");
thread.stop = true; // ①
thread.interrupt(); // ②
Thread.sleep(3000);
System.out.println("Stopping application...");
// System.exit( 0 );
}
}
public void run()
{
while (!stop)
{
System.out.println("Thread running...");
try
{
Thread.sleep(1000); // 模拟线程被阻塞
}
catch (InterruptedException e)
{
System.out.println("Thread interrupted...");
}
}
System.out.println("Thread exiting under request...");
}
}
java语句暂停
给你改了一下,在你所示的地方,加了一个线程。
---------------------------------------------------------------------------------------------------
public class xiancheng extends javax.swing.JFrame {
/** Creates new form xiancheng */
public xiancheng() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// GEN-BEGIN:initComponents
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(30, 40, 81, 25);
jButton2.setText("jButton2");
getContentPane().add(jButton2);
jButton2.setBounds(120, 100, 81, 25);
jButton2.setVisible(false);
jButton3.setText("jButton3");
getContentPane().add(jButton3);
jButton3.setBounds(190, 190, 81, 25);
jButton3.setVisible(false);
setMinimumSize(new java.awt.Dimension(300, 300));
pack();
}// /editor-fold
// GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton2.setVisible(true);
// Thread.sleep(5000);// -----在这里加线程暂停是没用的!请问要如何解决这个问题
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
jButton3.setVisible(true);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new xiancheng().setVisible(true);
}
});
}
// GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration//GEN-END:variables
}
java多线程为什么弃用stop和suspend
stop和suspend都有一些共同的点:都试图专横的控制一个给定了的线程的行为.从JDK1.2开始,这两个方法都被弃用了.stop天生就不安全,而经验告诉我们呢suspend方法会经常导致死锁。 stop这个方法将终止所有未结束的方法,包括run方法。
java中如何暂停一个程序?
1.
Thread.sleep(4000);
暂停4000毫秒,也就是四秒,如果像无限暂停那就把4000改成一个足够大的数.
* 注意引入Thread的包,Eclipse或Intellij会自动提示 (我忘了在哪个包了~
2.
可以用java.util.Scanner来要求用户输入东西以达到暂停效果,
具体使用方法请百度
java程序运行过程中如何暂停,恢复?
java控制程序执行,使用的是Thread这个类,可以控制程序暂停或者休眠几秒再执行。示例如下:
public abstract class MyThread extends Thread {
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);
}
}
javasuspent的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javasuspent的信息别忘了在本站进行查找喔。
发布于:2022-12-07,除非注明,否则均为
原创文章,转载请注明出处。