我要停止 线程为什么行不通.
package jsf;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class TwoCounter extends Thread {
private boolean started = false;
private TextField
t1 = new TextField(5),
t2 = new TextField(5);
private Label l = new Label("count1 == count2");
private int count1 = 0, count2 = 0;
// Add the display components as a panel
// to the given container:
public TwoCounter(Container c) {
Panel p = new Panel();
p.add(t1);
p.add(t2);
p.add(l);
c.add(p);
}
public void start() {
if(!started) {
started = true;
super.start();
}
}
public void run() {
while (true) {
synchronized(this){
t1.setText(Integer.toString(count1++));
t2.setText(Integer.toString(count2++));
try {
sleep(500);
}
catch (InterruptedException e) {}
}
}
}
public synchronized void synchTest() {
Counter2.incrementAccess();
if(count1 != count2)
l.setText("Unsynched");
}
}
class Watcher extends Thread {
private Counter2 p;
public Watcher(Counter2 p) {
this.p = p;
start();
}
public void run() {
while(true) {
for(int i = 0; i < p.s.length; i++)
{
p.s[i].synchTest();
}
try {
sleep(500);
} catch (InterruptedException e){}
System.out.print(p.s[0].getClass().getName());
try {
p.s[0]. wait(100);
}
catch (InterruptedException ex) {
}
}
}
}
public class Counter2 extends Applet {
TwoCounter[] s;
private static int accessCount = 0;
private static TextField aCount =
new TextField("0", 10);
public static void incrementAccess() {
accessCount++;
aCount.setText(Integer.toString(accessCount));
}
private Button
start = new Button("Start"),
observer = new Button("Observe");
private boolean isApplet = true;
private int numCounters = 0;
private int numObservers = 0;
public void init() {
Panel p = new Panel();
if(isApplet) {
numCounters = 2;
numObservers = 2;
}
s = new TwoCounter[numCounters];
for(int i = 0; i < s.length; i++)
{ s[i] = new TwoCounter(this);
}
start.addActionListener(new StartL());
p.add(start);
observer.addActionListener(new ObserverL());
p.add(observer);
p.add(new Label("Access Count"));
p.add(aCount);
add(p);
}
class StartL implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < s.length; i++)
s[i].start();
}
}
class ObserverL implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < numObservers; i++)
new Watcher(Counter2.this);
}
}
public static void main(String[] args) {
Counter2 applet = new Counter2();
// This isn't an applet, so set the flag and
// produce the parameter values from args:
applet.isApplet =false;
applet.numCounters = (args.length == 0 ? 5 : Integer.parseInt(args[0]));
applet.numObservers = (args.length < 2 ? 5 : Integer.parseInt(args[1]));
Frame aFrame = new Frame("Sharing1");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(350, applet.numCounters *100);
applet.init();
// applet.start();
aFrame.setVisible(true);
}
} ///:~
我要停止 TwoCounter 线程为什么行不通.
try {
p.s[0]. wait(100);
}
catch (InterruptedException ex) {
}
问题点数:20、回复次数:6Top
1 楼michalpan(潘)回复于 2006-03-02 11:18:29 得分 0
怎么没人回答我?Top
2 楼coolarmy(bb考拉猪)回复于 2006-03-02 11:27:03 得分 0
1.你创建Watcher以后并没有start
2.wait命令只是等待100而已,并不是停止TwoCounter线程Top
3 楼michalpan(潘)回复于 2006-03-02 11:46:27 得分 0
现在我wait 停止的是Watcher线程这是怎么回事
如果我要停止TwoCounter线程 应该是怎么写?Top
4 楼michalpan(潘)回复于 2006-03-03 13:00:04 得分 0
帮忙啊朋友Top
5 楼cleansunshing(努力学习中)回复于 2006-04-04 10:51:56 得分 0
好长...Top
6 楼freeman983(疾风暴走)回复于 2006-04-04 20:05:43 得分 0
可以用stop(),但这个方法现在不推荐使用,线程的生命周期是在run方法内,照这个思路,既如果跳出run方法,线程自然会死亡.如此,可用,break或return试试Top
相关问题
- 为什么行不通?
- 为什么这样行不通
- oracle问题:为什么update xxxtable set date = '1995-8-9' where……行不通呢?
- 这样为什么行不通,想不明白
- 高分求教:用ADO连接Access,为什么SQL语句“....like '*xxx*' ”行不通?
- 为什么我在SQL用的语句在这里行不通?在线等待!!!
- 多线程的程序,运行久了,有些线程会自动停止运行,这是为什么?如何不让他停止
- 三层,DCOM,请分析 if ClientDataSet1.UpdateStatus<>usUnModified then showmessage('有改动');这条语句为什么行不通?说usUnModified
- 为什么我用ado 的存储过程怎样也行不通?请各位高手帮忙!
- 在线程中SendMessage是否会导致线程的停止?




