当前位置:编程学习 > JAVA >>

求助。Java线程如何 开始和暂停,详细点啊 ,菜鸟没学过



用了suspend()和resume()可以实现,但是这两个方法过时了。

用了wait()没效果,暂停不了。

不要用sleep,因为我向要点击两个按钮,一个是开始线程(也是继续线程),一个是暂停线程,

而且要求是对同一个线程进行开始和暂停,不要new 一个新的线程、

求助。

PS:我试wait()和interrupt()不行的。。。 --------------------编程问答-------------------- wait()怎么可能不管用呢。你把代码发上来。 --------------------编程问答-------------------- 用sleep怎么就不能用两个按钮了。。。在开始按钮上写一个判断,再加一个变量。。。不就行了 --------------------编程问答-------------------- http://blog.csdn.net/perjer123/article/details/7371389
看看这篇文章吧。真的很不错。以前我也是只会用,懂得并不深,看完这个之后收获很大。 --------------------编程问答-------------------- 为什么wait不行?你有notify吗。。
最好最温柔的方式是用boolean来控制进程的结束。
--------------------编程问答-------------------- import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class Training extends JFrame implements Runnable, ActionListener {

public static JButton run;
public static JButton exit;
public static JButton stop;
public static Thread demo;
public static int i = 1,j=1;
//用到两个过时方法。还不知道怎么解决
public void run() {
while(true){
System.out.println(i++);
try {
demo.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

public static void main(String[] args) {
Training he = new Training();
demo = new Thread(he, "线程");
// demo.start();
 //demo.setDaemon(false);
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));

run = new JButton("运行");
exit = new JButton("退出");
stop = new JButton("停止");


run.addActionListener(he);
exit.addActionListener(he);
stop.addActionListener(he);

jp.add(run);
jp.add(stop);
jp.add(exit);
he.setLayout(new FlowLayout(FlowLayout.CENTER));
he.add(jp);
he.setLocation(300, 200);
he.setSize(200, 100);
he.setTitle("测试");
he.setVisible(true);

}

public void actionPerformed(ActionEvent b) {

if (b.getSource() == run) {
System.out.println("线程正在运行");

System.out.println(demo.getState().equals("BLOCKED"));



if (j == 1) {
demo.start();
j++;
}

return;
}
if (b.getSource() == exit) {
System.out.println("退出");

System.out.println(demo.activeCount());
System.exit(0);
return;
}
if (b.getSource() == stop) {
//
// try {
// demo.wait(11111);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
i = 1;
System.out.println("线程停止");
return;
}

}

}
--------------------编程问答-------------------- 用wait()和notify()就这样了

Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Training.actionPerformed(Training.java:86)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) --------------------编程问答-------------------- 改了一下代码,楼主参考:(去掉有关demo2的注释,可看见两个线程的效果)

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class Training extends JFrame implements Runnable, ActionListener
{
public static JButton run;
public static JButton exit;
public static JButton stop;
public static Thread demo;
//public static Thread demo2;
public static int i = 1,j=1;
private boolean waitFlag=false; //设置一标志,false暂停,true运行。
//用到两个过时方法。还不知道怎么解决
public void run() 
{
while(true)
{
System.out.println(""+Thread.currentThread().getName()+"  "+i++);
try 
{
//demo.sleep(1000);
Thread.sleep(1000);//静态方法。
}
        catch (InterruptedException e) 
{
e.printStackTrace();
}
while(!waitFlag)
{
System.out.println(Thread.currentThread().getName()+ " is pausing!");
pause();
System.out.println(Thread.currentThread().getName()+" is continuing!");
}
}
}

public  synchronized void pause()
{
try
{
wait();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
public synchronized void awake()
{
notifyAll();//用notify()只会唤醒一个等待线程
}
public static void main(String[] args) 
{
Training he = new Training();
demo = new Thread(he, "线程_1");
//demo2 = new Thread(he,"线程_2");
demo.start();
//demo2.start();

JPanel jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
run = new JButton("运行");
exit = new JButton("退出");
stop = new JButton("暂停");

run.addActionListener(he);
exit.addActionListener(he);
stop.addActionListener(he);

jp.add(run);
jp.add(stop);
jp.add(exit);
he.setLayout(new FlowLayout(FlowLayout.CENTER));
he.add(jp);
he.setLocation(300, 200);
he.setSize(200, 100);
he.setTitle("测试");
he.setVisible(true);
}

public void actionPerformed(ActionEvent b) 
{
if (b.getSource() == run) 
{
//System.out.println("线程正在运行");
waitFlag=true;
awake();
System.out.println("线程_1 的状态是 "+demo.getState());
//System.out.println("线程_2 的状态是 "+demo2.getState());
}
else if (b.getSource() == stop) 
{
waitFlag=false;
System.out.println("线程_1 的状态是 "+demo.getState());
//System.out.println("线程_2 的状态是 "+demo2.getState());
}
else if (b.getSource() == exit) 
{
System.out.println("退出");
System.out.println(demo.activeCount());
System.exit(0);
return;
}
}
}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,