当前位置: 代码迷 >> Java相关 >> [求助]急啊!关于线程的问题.
  详细解决方案

[求助]急啊!关于线程的问题.

热度:215   发布时间:2007-06-05 10:58:58.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());
}
}
}


----------------解决方案--------------------------------------------------------
太长了,我把中间JPanel的代码给切了,要紧吗?
----------------解决方案--------------------------------------------------------
SetFrame的代码呢?
----------------解决方案--------------------------------------------------------

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);
}
}


----------------解决方案--------------------------------------------------------

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());
}
}
}
他只会响应第一个线程


----------------解决方案--------------------------------------------------------
详细问题描述?
----------------解决方案--------------------------------------------------------
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);
这两个谁放前面谁有反应,但我要两个都有


----------------解决方案--------------------------------------------------------
1,程序设计不合理,你的起动线程的时候,根本就 没有管以前的线程有没有被销毁.并且你的run方法里面,没有sleep,这样会造成CPU瞬间使用量非常高
2,停止线程不能用stop方法
你的监听器没有必要注册那个多,一个就够了
----------------解决方案--------------------------------------------------------
  相关解决方案