[英]button event doesn't work properly
我對使用方法wait()和notify()有一些疑問。 我有下一個代碼,它有一些按鈕事件,第一次用戶按下它必須停止打印的按鈕,第二次重新開始打印。 我知道最好使用Runnable而不是Thread,但由於要求我必須使用Thread。 第一次按下按鈕時代碼工作正常,但第二次沒有,我想使用wait()和notify,但我不知道如何使用這個特定的代碼。
class Thr extends Thread{
private int count = 0;
private long pause;
private boolean canPrint = true;
private JTextArea textArea;
Thr(long miliseconds,JTextArea text){
pause = miliseconds;
textArea = text;
}
public void pushedButton(){
if(canPrint)
this.canPrint = false;
else
this.canPrint = true;
}
public void run()
{
while(this.canPrint)
{
try
{
this.printCounter();
Thread.sleep(pause);
this.count++;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void printCounter(){
String time;
time = Integer.toString(count);
textArea.setText(time);
}
}
class Interface extends JFrame implements ActionListener{
private JTextArea textArea,textArea2;
private JButton button;
private Thr thread,threadEvent;
Interface()
{
textArea = new JTextArea(10,7);
textArea2 = new JTextArea(10,7);
thread = new Thr(2000,textArea);
threadEvent = new Thr(1000,textArea2);
button = new JButton("Pausar/Reanudar");
this.getContentPane().add(button,BorderLayout.SOUTH);
this.getContentPane().add(textArea,BorderLayout.WEST);
this.getContentPane().add(textArea2,BorderLayout.EAST);
thread.start();
threadEvent.start();
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
threadEvent.pushedButton();
}
}
public class MensajesHilos {
public static void main(String[] args){
Interface i = new Interface();
i.setTitle("Control Threads");
i.setBounds(200, 200, 300, 240);
i.setVisible(true);
}
}
你編碼的方式,如果你想達到預期的結果,
我覺得修改需要在run方法中完成,
public void run()
{
while(true)
{
if(this.canPrint){
try
{
this.printCounter();
Thread.sleep(pause);
this.count++;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
通過這種方式,您的線程永遠不會死亡,並根據canPrint布爾值切換打印。
另外,請確保聲明canPrint變量volatile
,以便對其進行更改直接寫入主內存並立即反映。
“ 按鈕事件無法正常工作 ”
這是假的,如果你在actionPerformed
方法中放置一個print語句,你會看到每次按下按鈕時都會調用它。
順便提一下,您可以簡化這一點
if(canPrint)
this.canPrint = false;
else
this.canPrint = true;
至
this.canPrint = !this.canPrint;
請注意,這是一個很好的做法,始終把@Override
anotation上覆蓋方法的頂部。
@Override
public void actionPerformed(ActionEvent event)
{
threadEvent.pushedButton();
}
現在為什么你沒有得到預期的結果?
您可以省略調用thread.pushedButton
,因此canPrint
只會在threadEvent
對象中threadEvent
,並且永遠不會在thread
。
請注意,一旦布爾設置為false,您將退出loop
之后,即使你重新設置布爾值過程中不會啟動回true
。 此示例將使用while(true)
但是,您應該將true
更改為任何sentinel值以處理程序的退出,因為這將永遠循環。
@Override
public void run()
{
while(true)
{
if(this.canPrint)
{
this.printCounter();
this.count++;
}
try
{
Thread.sleep(pause);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
此外,請確保pause
永遠不會為0否則您將吃掉所有計算機進程。
請注意,正如其他所述,您應該將在線程中訪問的變量聲明為volatile
(canPrint)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.