当前位置: 技术问答>java相关
线程问题,高手指点:怎样暂停,继续线程
来源: 互联网 发布时间:2015-06-08
本文导语: 线程应该怎样暂停?在RUN()中用一个WHILE(TRUE)循环执行一段命令,把TRUE改成FALSE后线程停止了,线程也消亡了,那如何暂停后继续执行线程呢? 我想用WAIT(),和notify()控制,不过老是出错,不知道怎么用,有...
线程应该怎样暂停?在RUN()中用一个WHILE(TRUE)循环执行一段命令,把TRUE改成FALSE后线程停止了,线程也消亡了,那如何暂停后继续执行线程呢?
我想用WAIT(),和notify()控制,不过老是出错,不知道怎么用,有没有谁贴一段代码啊?
我想用WAIT(),和notify()控制,不过老是出错,不知道怎么用,有没有谁贴一段代码啊?
|
给个例子给你:自己换一些行;是关于银行存取款的。让
saver线程和spender线程同步。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Console
{
public static String title(Object o)
{
String t=o.getClass().toString();
if(t.indexOf("class") !=-1)
t=t.substring(6);
return t;
}
public static void setupClosing(JFrame frame){
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e{
System.exit(0);
}
});
}
public static void run(JApplet applet, int width, int height){
JFrame frame=new JFrame(title(applet));
setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(width,height);
applet.init();
applet.start();
frame.setVisible(true);
}
}
public class Banking extends JApplet
{
private static double balance=1000;
private JButton start=new JButton("start");
private boolean started=false;
private static JTextField curBalance=new JTextField("1000",10);
private static double saveBal=100;
private static double spenBal=9;
private static boolean runFlag=false;
private BankAccount bk;
private static boolean canget=true;
class BankAccount extends Thread
{
private JLabel lsv=new JLabel("money2 save/time"),
lsp=new JLabel("money2 spen/time");
private JTextField tsv=new JTextField("100",10),
tsp=new JTextField("9",10);
private Saver t=new Saver();
private Spender s=new Spender();
Object m=new Object();
public BankAccount()
{
JPanel p=new JPanel();
tsv.addActionListener(new SaveL());
tsp.addActionListener(new SpenL());
p.add(lsv);
p.add(tsv);
p.add(lsp);
p.add(tsp);
getContentPane().add(p);
}
public void start()
{
if(!started)
{
started=true;
super.start();
t.start();
s.start();
}
}
public synchronized void run()
{
while(true)
{
curBalance.setText(Double.toString(balance));
try {sleep(100);}
catch(InterruptedException e){System.err.println("INTERRUPTED");}
}
}
class Saver extends Thread
{
public Saver(){}
public void run()
{
while(true)
{
synchronized(m)
{
balance+=saveBal;
try{ sleep(1000);}catch(InterruptedException e){}
if(balance>spenBal)
{
canget=true;
m.notify();
}
}
}
}
}
class Spender extends Thread
{
public Spender(){}
public void run()
{
while(true)
{
synchronized(m)
{
if((balance-spenBal)
saver线程和spender线程同步。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Console
{
public static String title(Object o)
{
String t=o.getClass().toString();
if(t.indexOf("class") !=-1)
t=t.substring(6);
return t;
}
public static void setupClosing(JFrame frame){
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e{
System.exit(0);
}
});
}
public static void run(JApplet applet, int width, int height){
JFrame frame=new JFrame(title(applet));
setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(width,height);
applet.init();
applet.start();
frame.setVisible(true);
}
}
public class Banking extends JApplet
{
private static double balance=1000;
private JButton start=new JButton("start");
private boolean started=false;
private static JTextField curBalance=new JTextField("1000",10);
private static double saveBal=100;
private static double spenBal=9;
private static boolean runFlag=false;
private BankAccount bk;
private static boolean canget=true;
class BankAccount extends Thread
{
private JLabel lsv=new JLabel("money2 save/time"),
lsp=new JLabel("money2 spen/time");
private JTextField tsv=new JTextField("100",10),
tsp=new JTextField("9",10);
private Saver t=new Saver();
private Spender s=new Spender();
Object m=new Object();
public BankAccount()
{
JPanel p=new JPanel();
tsv.addActionListener(new SaveL());
tsp.addActionListener(new SpenL());
p.add(lsv);
p.add(tsv);
p.add(lsp);
p.add(tsp);
getContentPane().add(p);
}
public void start()
{
if(!started)
{
started=true;
super.start();
t.start();
s.start();
}
}
public synchronized void run()
{
while(true)
{
curBalance.setText(Double.toString(balance));
try {sleep(100);}
catch(InterruptedException e){System.err.println("INTERRUPTED");}
}
}
class Saver extends Thread
{
public Saver(){}
public void run()
{
while(true)
{
synchronized(m)
{
balance+=saveBal;
try{ sleep(1000);}catch(InterruptedException e){}
if(balance>spenBal)
{
canget=true;
m.notify();
}
}
}
}
}
class Spender extends Thread
{
public Spender(){}
public void run()
{
while(true)
{
synchronized(m)
{
if((balance-spenBal)