为什么stop按纽停止线程不能停止??
//TimeThread.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TimeThread extends JFrame
{
public static JTextField textField;
private JButton button1,button2;
private boolean start=true;
Thread t;
int flag=1;
public TimeThread()
{
super("Time Thread test");
Container c=getContentPane();
c.setLayout(new FlowLayout());
textField=new JTextField(10);
textField.setEditable(false);
c.add(textField);
button1=new JButton("start");
button2=new JButton("stop");
c.add(button1);
c.add(button2);
TimeThread1 thread=new TimeThread1();
t=new Thread(thread);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(start && flag==1)
{
t.start();
start=false;
}
else
{
t.resume();
start=false;
}
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(Thread.currentThread().getName().equals("Thread-1"))
//if(t.getName().equals("Thread-0")) //here why wrong
{
t.suspend();
start=true;
flag++;
}
}
}); setSize(180,150);
show();
}
public static void main(String[] args)
{
TimeThread t=new TimeThread();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}//TimeThread1.java
import java.util.*;
public class TimeThread1 implements Runnable
{
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
Date date=new Date();
String s=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
TimeThread.textField.setText(s);
}
}
catch(Exception e){}
}
}
//TimeThread.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TimeThread extends JFrame
{
public static JTextField textField;
private JButton button1,button2;
private boolean start=true;
Thread t;
int flag=1;
public TimeThread()
{
super("Time Thread test");
Container c=getContentPane();
c.setLayout(new FlowLayout());
textField=new JTextField(10);
textField.setEditable(false);
c.add(textField);
button1=new JButton("start");
button2=new JButton("stop");
c.add(button1);
c.add(button2);
TimeThread1 thread=new TimeThread1();
t=new Thread(thread);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(start && flag==1)
{
t.start();
start=false;
}
else
{
t.resume();
start=false;
}
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(Thread.currentThread().getName().equals("Thread-1"))
//if(t.getName().equals("Thread-0")) //here why wrong
{
t.suspend();
start=true;
flag++;
}
}
}); setSize(180,150);
show();
}
public static void main(String[] args)
{
TimeThread t=new TimeThread();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}//TimeThread1.java
import java.util.*;
public class TimeThread1 implements Runnable
{
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
Date date=new Date();
String s=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
TimeThread.textField.setText(s);
}
}
catch(Exception e){}
}
}
一旦开始就一直执行。
线程类中加一个stop()
void stop()
{
flag=false;
}else
{
t.resume();
start=false;
}
改为
else
t.stop();while(flag)
{
....
}
我用if(Thread.currentThread()==t)
好象也不对啊??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;public class TimeThread extends JFrame
{
public static JTextField textField;
private JButton button1,button2;
private boolean start=true;
Thread t;
int flag=1;
public TimeThread()
{
super("Time Thread test");
Container c=getContentPane();
c.setLayout(new FlowLayout());
textField=new JTextField(10);
textField.setEditable(false);
c.add(textField);
button1=new JButton("start");
button2=new JButton("stop");
c.add(button1);
c.add(button2);
TimeThread1 thread=new TimeThread1();
t=new Thread(thread);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(start && flag==1)
{
t.start();
start=false;
}
else
{
t.resume();
start=false;
}
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.suspend();
start=true;
flag++;
}
}); setSize(180,150);
show();
}
public static void main(String[] args)
{
TimeThread t=new TimeThread();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
class TimeThread1 implements Runnable
{
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
Date date=new Date();
String s=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
TimeThread.textField.setText(s);
}
}
catch(Exception e){}
}
}
简单点,你可以中断thread,需要重新启动的时候,再重新new一个thread
复杂点,你可以设置一个状态开关变量,一旦被设置为‘暂停’,就进入一个循环,每此循环sleep 3秒,然后看看新的状态,如果新状态是‘继续’,那就跳出循环,如果是‘结束’,那就return了。
if(Thread.currentThread().getName().equals("AWT-EventQueue-0"))
就可以实现:
按了“Start”按钮时间开始走动,按了“Stop”时间显示停止。
你如果这个程序没有窗体,你用字母来控制启动和停止,你的if(Thread.currentThread().getName().equals("Thread-1"))应该可以实现的。
旧行,为什么这个不行因为你的县城的名字不对,用Thread(String name)给县城一个名字
Slist->S;Slist|S|{Slist}
S-> V=E|if B then Slist else Slist
|return(E) |Decl|Funcd|Callf
E->E+T|T
T->T*F|F
F->(E)|V|NUM
B->E>E|E<E
Decl->I
int Vlist
Vlist->V,Vlist|V
Funcd->V(Param){Slist}
Param->Decl
Callf->V(Vlist)其中:
V代表字母开头的,后跟字母和数字的变量名。
NUM代表整数。编译原理实验题目
一、 词法分析程序
要求:能够处理文本格式的,符合编译原理实验语法的源程序。
输出形式为:二元组:(单词种类,单词内码值)
或三元组:(单词种类,单词内码值,源程序中的行号)******注:单词种类统一分为:
单词符号 单词种类
任意变量名 0
( 1
) 2
{ 3
} 4
; 5
= 6
+ 7
* 8
> 9
< 10
, 11
整型常数 20
int 21
if 22
then 23
else 24
return 25
出错 100
二、 语法分析程序
要求:能判别正确的源程序;对错误的源程序,尽量指出错误类型(参见各种分析方法中的出错处理一节)。三、 编程工具: 任选自己熟悉的一种。