/*
* 要求,三个线程来同时产生三个随机数,用一个按钮来控制开始摇奖和停止摇奖
* 在开始摇奖和停止摇奖之间用线程的暂停和唤醒来实现!
*
* 请大家帮我改下或给些建议,谢谢!
* */
// 下面红色部分为有问题的地方,后面总抛出异常
/*
* Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:571)
at yaojiang.APanel$1.actionPerformed(FormMain.java:83)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
* */
/*
*
Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException: current thread not owner 线程不能自己调用wait()方法吗?
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:474)
at yaojiang.APanel$1.actionPerformed(FormMain.java:139)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(BasicButtonListener.java:285)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1571)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2763)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2798)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2726)
at java.awt.Component.processEvent(Component.java:5265)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:672)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:920)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:798)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:636)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
* */
FormMain 类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FormMain
{
public static void main(String[] args)
{
AFrame frame = new AFrame("摇奖机");
frame.setLayout(null);
frame.setSize(400,200);
frame.setVisible(true);
}
}
class AFrame extends JFrame
{
public AFrame(String name)
{
super(name);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
APanel pan = new APanel();
setContentPane(pan);
}
}
class APanel extends JPanel
{
private JLabel lblNum1 = new JLabel("0");
private JLabel lblNum2 = new JLabel("0");
private JLabel lblNum3 = new JLabel("0");
private JButton btnStart = new JButton("开 始");
private RandomNum rn1 = new RandomNum(lblNum1);
private RandomNum rn2 = new RandomNum(lblNum2);
private RandomNum rn3 = new RandomNum(lblNum3);
private Thread th1 = new Thread(rn1);
private Thread th2 = new Thread(rn2);
private Thread th3 = new Thread(rn3);
private boolean startFlag = true;
public APanel()
{
lblNum1.setBounds(50, 50, 20, 20);
lblNum2.setBounds(100, 50, 20, 20);
lblNum3.setBounds(150, 50, 20, 20);
btnStart.setBounds(80,100,100,30);
add(lblNum1);
add(lblNum2);
add(lblNum3);
add(btnStart);
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(startFlag)
{
JOptionPane.showMessageDialog(null, "外if"+th1.getState().toString());
if(th1.getState()==Thread.State.NEW &&
th2.getState()==Thread.State.NEW &&
th3.getState()==Thread.State.NEW)
{
JOptionPane.showMessageDialog(null, "内if"+th1.getState().toString());
th1.start();
th2.start();
th3.start();
startFlag = false;
btnStart.setText("停止");
}
else
{
JOptionPane.showMessageDialog(null, "内else"+th1.getState().toString());
rn1.startRun();
rn2.startRun();
rn3.startRun();
th1.notify();
th2.notify();
th3.notify();
startFlag = false;
btnStart.setText("停止");
}
}
else
{
JOptionPane.showMessageDialog(null, "外else"+th1.getState().toString());
rn1.stopRun();
rn2.stopRun();
rn3.stopRun();
try
{
th1.wait();
th2.wait();
th3.wait();
/*
如果把红色的去掉则可以正常停止随机数产生,并停住显示出来,但这样一来,又不符合老师要求用线程等待和唤醒来实现的要求.
即使这三句红色的,在上面该如何恢复线程继续从新产生随机数呢?如果再th1.start()也会抛了异常....请高手帮忙看下,谢谢!
*/
}
catch(InterruptedException ire)
{
ire.printStackTrace();
}
startFlag = true;
btnStart.setText("开始");
}
}
});
}
}
RandomNum 类
import javax.swing.*;
public class RandomNum implements Runnable
{
private boolean runFlag = true;
private JLabel lblnum1;
public RandomNum(JLabel lbl)
{
lblnum1 = lbl;
}
public void run()
{
while(runFlag)
{
int num = (int)((Math.random()*100)%10);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
lblnum1.setText(String.valueOf(num));
}
}
public void startRun()
{
runFlag = true;
}
public void stopRun()
{
runFlag = false;
}
}