| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1238 人关注过本帖
标题:[求助]急啊!关于线程的问题.
只看楼主 加入收藏
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
得分:0 

哪位高手帮帮忙啊?

2007-06-05 10:58
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
先把全部的代码帖出来
我们得知道你是如何起动线程的,

可惜不是你,陪我到最后
2007-06-05 11:30
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
得分:0 

谢谢你啊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();

sf.setSize(1000,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionTwo actTwo=new ActionTwo(sf);
ActionOne actOne=new ActionOne(sf);

}
}

class ActionOne extends JFrame implements Runnable{
SetFrame sf;
Thread runnerOne;
int floor=0;
int floor_end=1;
int person_num=0;
public ActionOne(SetFrame stfm){
sf=stfm;
sf.liftOne.ss.jlb[0].setForeground(Color.GREEN);//开始第一层为绿色
for(int i=0;i<20;i++){
sf.liftOne.sbp.jbt[i].addActionListener(new MyActionListener());
sf.liftOne.sbp.jbt[i].setActionCommand(""+(i+1));
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftOne.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多20人
threadStart();
}
}
public void threadStart(){
runnerOne=new Thread(this);
runnerOne.start();
}
public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}

}
}
public void up(){
sf.liftOne.sp.setState(true);
sf.liftOne.sp.SetNum(person_num);
pause(1000);
sf.liftOne.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
sf.liftOne.sp.setState(true);
sf.liftOne.sp.SetNum(person_num);
pause(1000);
sf.liftOne.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.liftOne.ss.jlb[0].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.liftOne.sp.setState(false);
sf.liftOne.sbp.jbt[floor-1].setForeground(Color.BLACK);
runnerOne.stop();
}
public void pause(int time){
try{
runnerOne.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}


class ActionTwo extends JFrame implements Runnable{
SetFrame sf;
Thread runner;
int floor=0;
int floor_end=1;
int person_num=0;
public ActionTwo(SetFrame stfm){
sf=stfm;
sf.liftTwo.ss.jlb[0].setForeground(Color.GREEN);//开始第一层为绿色
for(int i=0;i<20;i++){
sf.liftTwo.sbp.jbt[i].addActionListener(new MyActionListener());
sf.liftTwo.sbp.jbt[i].setActionCommand(""+(i+1));
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftTwo.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多20人
threadStart();
}
}
public void threadStart(){
runner=new Thread(this);
runner.start();
}

public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}

}
}
public void up(){
sf.liftTwo.sp.setState(true);
sf.liftTwo.sp.SetNum(person_num);
pause(1000);
sf.liftTwo.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
sf.liftTwo.sp.setState(true);
sf.liftTwo.sp.SetNum(person_num);
pause(1000);
sf.liftTwo.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.liftTwo.ss.jlb[0].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.liftTwo.sp.setState(false);
sf.liftTwo.sbp.jbt[floor-1].setForeground(Color.BLACK);
runner.stop();
}
public void pause(int time){
try{
runner.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}

2007-06-05 11:37
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
得分:0 
太长了,我把中间JPanel的代码给切了,要紧吗?
2007-06-05 11:40
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
SetFrame的代码呢?

可惜不是你,陪我到最后
2007-06-05 11:41
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
得分:0 

public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();

sf.setSize(1000,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionTwo actTwo=new ActionTwo(sf);
ActionOne actOne=new ActionOne(sf);

}
}

class SetButtonPanel extends JPanel{
JButton jbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetButtonPanel(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
jbt[i]=new JButton(""+(i+1));
add(jbt[i]);
}
}
}

class SetShow extends JPanel{
JLabel jlb[]=new JLabel[10];
public SetShow(){
setLayout(new GridLayout(2,5));
for(int i=0;i<10;i++){
jlb[i]=new JLabel(""+(i+1)+"层");
jlb[i].setFont(new Font("",Font.BOLD,12));
add(jlb[i]);
}
}
}

class StringPanel extends JPanel{
JLabel state=new JLabel("门关上了");
JLabel num=new JLabel("目前人数");
public StringPanel(){
// setLayout(new FlowLayout(FlowLayout.CENTER));
state.setFont(new Font("宋体",Font.BOLD,20));
state.setForeground(Color.RED);
num.setFont(new Font("宋体",Font.BOLD,20));
num.setForeground(Color.BLUE);
add(state);
add(num);
}
public void setState(boolean stt){
if(stt==true){
state.setText("门关上了");
}else{
state.setText("门开了");
}
}
public void SetNum(int n){
num.setText("目前人数: "+n+"人");
}
}

class LiftOne extends JPanel{
SetButtonPanel sbp=new SetButtonPanel();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public LiftOne(){
setLayout(new BorderLayout(5,5));
add(ss,BorderLayout.NORTH);
add(sbp,BorderLayout.EAST);
add(sp,BorderLayout.CENTER);
}
}

class LiftTwo extends JPanel{
SetButtonPanel sbp=new SetButtonPanel();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public LiftTwo(){
setLayout(new BorderLayout(5,5));
add(ss,BorderLayout.NORTH);
add(sbp,BorderLayout.EAST);
add(sp,BorderLayout.CENTER);
}
}

class SetFrame extends JFrame{
Container con=getContentPane();
LiftOne liftOne = new LiftOne();
LiftTwo liftTwo = new LiftTwo();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(liftOne,BorderLayout.EAST);
con.add(liftTwo,BorderLayout.WEST);
}
}

2007-06-05 11:46
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
得分:0 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();

sf.setSize(1000,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionTwo actTwo=new ActionTwo(sf);
ActionOne actOne=new ActionOne(sf);

}
}

class SetButtonPanel extends JPanel{
JButton jbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetButtonPanel(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
jbt[i]=new JButton(""+(i+1));
add(jbt[i]);
}
}
}

class SetShow extends JPanel{
JLabel jlb[]=new JLabel[10];
public SetShow(){
setLayout(new GridLayout(2,5));
for(int i=0;i<10;i++){
jlb[i]=new JLabel(""+(i+1)+"层");
jlb[i].setFont(new Font("",Font.BOLD,12));
add(jlb[i]);
}
}
}

class StringPanel extends JPanel{
JLabel state=new JLabel("门关上了");
JLabel num=new JLabel("目前人数");
public StringPanel(){
// setLayout(new FlowLayout(FlowLayout.CENTER));
state.setFont(new Font("宋体",Font.BOLD,20));
state.setForeground(Color.RED);
num.setFont(new Font("宋体",Font.BOLD,20));
num.setForeground(Color.BLUE);
add(state);
add(num);
}
public void setState(boolean stt){
if(stt==true){
state.setText("门关上了");
}else{
state.setText("门开了");
}
}
public void SetNum(int n){
num.setText("目前人数: "+n+"人");
}
}

class LiftOne extends JPanel{
SetButtonPanel sbp=new SetButtonPanel();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public LiftOne(){
setLayout(new BorderLayout(5,5));
add(ss,BorderLayout.NORTH);
add(sbp,BorderLayout.EAST);
add(sp,BorderLayout.CENTER);
}
}

class LiftTwo extends JPanel{
SetButtonPanel sbp=new SetButtonPanel();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public LiftTwo(){
setLayout(new BorderLayout(5,5));
add(ss,BorderLayout.NORTH);
add(sbp,BorderLayout.EAST);
add(sp,BorderLayout.CENTER);
}
}

class SetFrame extends JFrame{
Container con=getContentPane();
LiftOne liftOne = new LiftOne();
LiftTwo liftTwo = new LiftTwo();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(liftOne,BorderLayout.EAST);
con.add(liftTwo,BorderLayout.WEST);
}
}

class ActionOne extends JFrame implements Runnable{
SetFrame sf;
Thread runnerOne;
int floor=0;
int floor_end=1;
int person_num=0;
public ActionOne(SetFrame stfm){
sf=stfm;
sf.liftOne.ss.jlb[0].setForeground(Color.GREEN);//开始第一层为绿色
for(int i=0;i<20;i++){
sf.liftOne.sbp.jbt[i].addActionListener(new MyActionListener());
sf.liftOne.sbp.jbt[i].setActionCommand(""+(i+1));
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftOne.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多20人
threadStart();
}
}
public void threadStart(){
runnerOne=new Thread(this);
runnerOne.start();
}
public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}

}
}
public void up(){
sf.liftOne.sp.setState(true);
sf.liftOne.sp.SetNum(person_num);
pause(1000);
sf.liftOne.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
sf.liftOne.sp.setState(true);
sf.liftOne.sp.SetNum(person_num);
pause(1000);
sf.liftOne.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.liftOne.ss.jlb[0].setForeground(Color.GREEN);
sf.liftOne.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.liftOne.sp.setState(false);
sf.liftOne.sbp.jbt[floor-1].setForeground(Color.BLACK);
runnerOne.stop();
}
public void pause(int time){
try{
runnerOne.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}


class ActionTwo extends JFrame implements Runnable{
SetFrame sf;
Thread runner;
int floor=0;
int floor_end=1;
int person_num=0;
public ActionTwo(SetFrame stfm){
sf=stfm;
sf.liftTwo.ss.jlb[0].setForeground(Color.GREEN);//开始第一层为绿色
for(int i=0;i<20;i++){
sf.liftTwo.sbp.jbt[i].addActionListener(new MyActionListener());
sf.liftTwo.sbp.jbt[i].setActionCommand(""+(i+1));
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftTwo.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多20人
threadStart();
}
}
public void threadStart(){
runner=new Thread(this);
runner.start();
}

public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}

}
}
public void up(){
sf.liftTwo.sp.setState(true);
sf.liftTwo.sp.SetNum(person_num);
pause(1000);
sf.liftTwo.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
sf.liftTwo.sp.setState(true);
sf.liftTwo.sp.SetNum(person_num);
pause(1000);
sf.liftTwo.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.liftTwo.ss.jlb[0].setForeground(Color.GREEN);
sf.liftTwo.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.liftTwo.sp.setState(false);
sf.liftTwo.sbp.jbt[floor-1].setForeground(Color.BLACK);
runner.stop();
}
public void pause(int time){
try{
runner.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}
他只会响应第一个线程

2007-06-05 11:52
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
详细问题描述?

可惜不是你,陪我到最后
2007-06-05 11:54
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
得分:0 
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.liftTwo.sbp.jbt[floor-1].setForeground(Color.BLUE);
person_num=(int)(1+Math.random()*20);//电梯最多20人
threadStart();
}
}
刚才还有一个版主说,因为我的启动是单击启动.所以运行后只能有一部电梯有反应.
问题是ActionTwo actTwo=new ActionTwo(sf);
ActionOne actOne=new ActionOne(sf);
这两个谁放前面谁有反应,但我要两个都有

2007-06-05 12:00
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
1,程序设计不合理,你的起动线程的时候,根本就 没有管以前的线程有没有被销毁.并且你的run方法里面,没有sleep,这样会造成CPU瞬间使用量非常高
2,停止线程不能用stop方法
你的监听器没有必要注册那个多,一个就够了

可惜不是你,陪我到最后
2007-06-05 12:13
快速回复:[求助]急啊!关于线程的问题.
数据加载中...
 
   



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

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