| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3229 人关注过本帖
标题:[求助]多线程产生随机数问题..
取消只看楼主 加入收藏
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
收藏
 问题点数:0 回复次数:4 
[求助]多线程产生随机数问题..


/*
* 要求,三个线程来同时产生三个随机数,用一个按钮来控制开始摇奖和停止摇奖
* 在开始摇奖和停止摇奖之间用线程的暂停和唤醒来实现!
*
* 请大家帮我改下或给些建议,谢谢!
* */

// 下面红色部分为有问题的地方,后面总抛出异常

/*
* 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;
}
}

搜索更多相关主题的帖子: 随机数 线程 
2007-01-26 21:55
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
收藏
得分:0 

来个兄弟拉小弟一把呀..


我在说's blog   http://www..cn
2007-01-26 22:18
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
收藏
得分:0 

NO!....还没有人来帮下我...崩溃了....


我在说's blog   http://www..cn
2007-01-26 23:14
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
收藏
得分:0 

好,谢谢千里....

我仔细研究一下..


我在说's blog   http://www..cn
2007-01-28 09:47
limaoyuan
Rank: 1
等 级:新手上路
帖 子:493
专家分:0
注 册:2006-6-25
收藏
得分:0 

果然是千里...谢谢了..学到了..

wait和notify要在静态块或静态方法中使用

还有就是判断比较巧妙,创建线程马上用wait,在开始的时候notifyAll,这想法比较和我不一样,这就是差距啊...
历害..

再次感谢..支持千里


我在说's blog   http://www..cn
2007-01-28 10:11
快速回复:[求助]多线程产生随机数问题..
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029950 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved