Urgent:Codes about Multi-Threading Programming


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Urgent:Codes about Multi-Threading Programming

  1. #1
    Join Date
    Jun 2004
    Posts
    7

    Urgent:Codes about Multi-Threading Programming

    Hi,
    I intend to make my program run a dead loop by clicking a button component.While this process is going on, and I try to click a JLabel component(of course,this action can trigger another thread.),sometimes at this moment my program will collapse, followed by lots of exceptions.(However,it occurs just occasionally.)
    My codes are listed as followsThe exceptions will be listed in another post.)

    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Calendar;
    import java.util.Date;

    public class test extends JFrame implements ActionListener ,MouseMotionListener,MouseListener
    {
    public static void main(String[] args)
    {
    test frame=new test();
    }

    JLabel lbb1[]=new JLabel[100];
    JFrame frame1;
    JPanel pane1;
    JButton button1;

    public t68()
    {

    frame1=new JFrame("");
    Container c=frame1.getContentPane();
    c.setLayout(null);
    c.setBackground(Color.white);

    pane1=new JPanel();
    pane1=new JScrollPane();
    pane1.setBackground(new Color(247,247,240));
    pane1.setLayout(null);
    pane1.setSize(1280,200);
    pane1.setLocation(0,0);
    pane1.addMouseListener(this);
    c.add(pane1);

    lbb1[a]=new JLabel();
    lbb1[a].setOpaque(true);
    lbb1[a].setSize(60,20);
    lbb1[a].setFont(new java.awt.Font("幼圆",0,15));
    lbb1[a].setBorder(BorderFactory.createRaisedBevelBorder());
    lbb1[a].setVisible(false);
    lbb1[a].addActionListener(this);
    lbb1[a].addMouseListener(this);
    lbb1[a].addMouseMotionListener(this);

    button1=new JButton();
    button1.setLocation(100,20);
    button1.setSize(80,20);
    button1.addMouseListener(this);
    button1.addMouseMotionListener(this);
    button1.addActionListener(this);

    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(1280,1028);
    frame1.setVisible(true);


    }


    public void mousePressed(MouseEvent e){
    for(int i=0;i<20;i++){
    if(e.getSource()==lbb1[i]){
    frame2=new JFrame("");
    Container c2=frame2.getContentPane();
    c2.setLayout(null);

    pn2=new JPanel();
    pn2.setSize(629,182);
    pn2.setLayout(null);
    c2.add(pn2);

    llb1=new JLabel("123");
    llb1.setSize(50,20);
    llb1.setLocation(30,15);
    llb1.setFont(new java.awt.Font("幼圆",0,16));
    pn2.add(llb1);

    llb2=new JLabel("234");
    llb2.setSize(50,20);
    llb2.setLocation(171,15);
    llb2.setFont(new java.awt.Font("幼圆",0,16));
    pn2.add(llb2);

    frame2.setSize(629,182);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension Frame2Size = frame2.getSize();
    frame2.setLocation((screenSize.width - Frame2Size.width)/2-(frame2.getX()/2),(screenSize.height - Frame2Size.height)/2-(frame2.getY()/2));
    frame2.dispose();
    frame2.setResizable(false);
    frame2.setVisible(true);


    }

    }

    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseDragged(MouseEvent e){}
    ...........



    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==button1){
    try{

    newThread Thread1=new newThread(1,"k123");
    Thread1.start();

    }
    catch(Exception e123){
    System.out.println(e123.toString());
    }
    }
    }

    class newThread extends Thread{
    String Tname;
    int s;
    public newThread(int x,String y){
    Tname=y;
    s=x;
    }
    public void run()
    {
    database newDatabase=new database();
    int reachT=newDatabase.IntTime("select time from schedule WHERE train='"+Tname+"'AND sign=1");
    String Trainnum=newDatabase.TrainNum("select trainname from attribute WHERE train='"+Tname+"'AND group="+Tgroup+" ORDER BY position ASC");
    int outT=newDatabase.IntTime("select time from schedule WHERE train='"+Tname+"'AND sign=3");
    String num[]=Trainnum.split(" ");
    int numn=num.length;

    int y=1;
    while(y==1){
    cal.setTime(new Date());
    int ddate=cal.get(Calendar.DATE);
    int hhour=cal.get(Calendar.HOUR_OF_DAY);
    int mmin=cal.get(Calendar.MINUTE);
    int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    int Infactmonth=month+1;
    int tdate3=0;
    int second=cal.get(Calendar.SECOND);


    float tdate1=(float)reachT/(24*60);
    int tdate2=reachT/(24*60);
    if(tdate1>tdate2){
    tdate3=24*60*(tdate2+1);
    System.out.println("tdate3="+tdate3);
    }
    //若跨月
    if(ddate-Tinitdate<0){
    if(Infactmonth>InitMonthnum){
    InfactTime=(Datenum-Tinitdate+ddate)*24*60+hhour*60+mmin;

    }
    else if(Infactmonth==InitMonthnum)
    if(Tinitdate-ddate==1){
    InfactTime=tdate2*60*24+hhour*60+mmin;

    }
    }
    else{
    //仅跨天或为当天,
    InfactTime=(ddate-Tinitdate)*60*24+hhour*60+mmin;

    }
    //还没出发
    if(InfactTime<outT){
    for(int k1=0;k1<numn;k1++)
    {
    lbb1[k1].setVisible(true);
    lbb1[k1].setLocation(80+60*k1,10+50*(StationNum-1));
    lbb1[k1].setText(num[k1]);
    pane1.add(lbb1[k1]);
    }
    }

    if(InfactTime==eStationRtime){
    for(int k1=0;k1<numn;k1++){
    lbb1[k1].setVisible(true);
    lbb1[k1].setLocation(80+60*k1+80,10);
    lbb1[k1].setText(num[k1]);
    pane1.add(lbb1[k1]);
    }
    }

    if(InfactTime>reachT&&InfactTime<tdate3+outT){
    for(int k1=0;k1<numn;k1++){
    lbb1[k1].setVisible(true);
    lbb1[k1].setLocation(80+60*k1,15+50*(StationNum-1));
    lbb1[k1].setText(num[k1]);
    pane1.add(lbb1[k1]);
    }
    }

    }
    }
    }


    Hope you would help me,thank you in advance!

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    what does "run a dead loop" do? are you trying to make your program wait? why?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center