0

I have created a GUI for starting a Thread which does something very simple. However, the child thread never starts.

The child thread, if started, will give some output; I don't get any output though. What am I missing?

Here's the Code:

  1. The GUI class:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    
    public class gui {
        public static void main(String [] args) {           
            //final standalone s = new standalone();
            final try2 t= new try2();
    
            JFrame win = new JFrame();
            win.setLayout(new FlowLayout());
    
            JButton start = new JButton("Start");
            JButton stop = new JButton("Stop");
    
            start.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    t.t.start();
                    System.out.println("M on!");
                    try{
                    Thread.currentThread().sleep(10000);
                    }catch(Exception e1)
                    {
                        e1.printStackTrace();
                    }
                    System.out.println("M off!");
                    if(t.t.isInterrupted())
                        System.out.println("Stopped");
                }
            });         
    
            win.add(start);
            win.add(stop);
    
            win.setVisible(true);
        }
    }
    
  2. And here is the Child Thread

    public class try2 implements Runnable {
        public Thread t;
        int i;
    
        try2() {
            t=new Thread();             
        }
    
        public void run() {
            System.out.println(++i);
        }
    }
    
bezmax
  • 25,562
  • 10
  • 53
  • 84
nikel
  • 3,402
  • 11
  • 45
  • 71

3 Answers3

3

When you call t.t.start() it is starting the thread object in the t field of your try2 object. Unfortunately, this Thread has no Runnable, so when you start it, it will exit immediately. The try2.run() method is not called because the thread knows nothing about it.

Your code is convoluted. I'd simplify / fix it as follows:

  1. Get rid of the try2.t field.

  2. In the actionPerformed method create and run the thread as follows:

      new Thread(t).start();
    

    where t is your try2 instance.

And while you are fixing the code, try2 violates all Java style guides that I've ever come across. Class names should always start with a capital letter. Get into the habit of doing it right ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Make your class try2 extends Thread (and remove the implements Runnable)., then simply call start() on your try2 instance.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • 1
    Extending `Thread` is not the best practice. You should rather implement `Runnable` and put it into `Thread`'s constructor. Here's explanation: http://stackoverflow.com/questions/541487/java-implements-runnable-vs-extends-thread – bezmax Mar 21 '12 at 07:52
  • That would be my preference too, but for such a simple case, it is a bit of an overkill. – Guillaume Polet Mar 21 '12 at 08:02
  • 1
    Not really. Instead of `threadObject.start()` you write `new Thread(runnableObject).start()`. Not that much of an overkill. An overkill would be using `ExecutorService` (which is actually preferred) for such simple task. – bezmax Mar 21 '12 at 08:04
0

Your class try2 should extend Thread (and implement the method run()) The way you are dealing with it, yuo are calling the run()-method of the thread-Object t inside try2. But the run()-method of this object is empty.

AlexS
  • 5,295
  • 3
  • 38
  • 54
  • Extending `Thread` is a bad idea. Implementing `Runnable` is a preferred way. More info on that here: http://stackoverflow.com/questions/541487/java-implements-runnable-vs-extends-thread – bezmax Mar 21 '12 at 07:53