for(int i=1;i<500000000;i++)
for(int j=1;j<4;j++) {}
这些在调用线程的函数里实现是不对的
MeasureWaitWnd wnd2 = new MeasureWaitWnd(300,200,200,200);
wnd2.start();
后来你又重新创建了一个线程的实例,那你的继承Thread的类派什么用呢?应该在你的线程类里重载run方法,在执行好后调用sleep方法,设定好睡眠时间,这样就可以重复运行了
for(int j=1;j<4;j++) {}
这些在调用线程的函数里实现是不对的
MeasureWaitWnd wnd2 = new MeasureWaitWnd(300,200,200,200);
wnd2.start();
后来你又重新创建了一个线程的实例,那你的继承Thread的类派什么用呢?应该在你的线程类里重载run方法,在执行好后调用sleep方法,设定好睡眠时间,这样就可以重复运行了
这个线程执行就不行了,画面的文字不再变动了package showWindow;import javax.swing.*;
import java.awt.event.*;
import java.awt.*;public class MeasureWaitWnd extends Thread
{
public static boolean b = false;
public static Label lab;
public static JFrame w;
public MeasureWaitWnd(int sizex,int sizey,int locx,int locy)
{
lab = new Label(" Measure......");
Font f = new Font(lab.getText(),Font.ITALIC,30);
lab.setFont(f);
lab.setForeground(new Color(100,255,0));
lab.setBackground(new Color(0,0,255));
w = new JFrame();
w.setSize(sizex,sizey);
w.setLocation(locx,locy);
/*
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
*/
//1.DO_NOTHING_ON_CLOSE - do not do anything - require the program to handle the operation in the windowClosing method of a registered WindowListener object.
//2.HIDE_ON_CLOSE - automatically hide the frame after invoking any registered WindowListener objects
//3.DISPOSE_ON_CLOSE - automatically hide and dispose the frame after invoking any registered WindowListener objects //f.setDefaultCloseOperation(3);
Container c = w.getContentPane();
c.setLayout( new BorderLayout() );
c.add("Center",lab);
w.setVisible(true);
try
{
sleep(1000);
}
catch(InterruptedException e)
{
//
}
}
public void run()
{
String show = "Measure";
for(int i=1;i<100;i++)
{
if(b)
{
w.dispose();
return;
}
int j=i%10;
switch(j)
{
case 0:show = "Measure.";
break;
case 1:show = "Measure..";
break;
case 2:show = "Measure...";
break;
case 3:show = "Measure....";
break;
case 4:show = "Measure.....";
break;
case 5:show = "Measure......";
break;
case 6:show = "Measure.......";
break;
case 7:show = "Measure........";
break;
case 8:show = "Measure.........";
break;
}
lab.setText(" "+show);
try
{
sleep(200);
}
catch(InterruptedException e)
{
//
}
}
w.dispose();
return;
}
}
问题在于,b是一个static变量,已经是true了,所以第二个线程起来之后,很快就执行完毕了。
也确实,我只是写出来学习多线程的,以后一定多注意的,谢谢