Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm pretty much new to threads.I wanted to create some simple function working separately from main thread.But it doesn't seem to work.I'd just like to create new thread and do some stuff there independently of what's happening on main thread.I know this code may look weird but I don't have much experiences with threading so far.Could you guys explain me what's wrong with this?Thank you!

      public static void main(String args[]){
      test z=new test();

      z.setBackground(Color.white);

      frame=new JFrame();
      frame.setSize(500,500);
      frame.add(z);
      frame.addKeyListener(z);
      frame.setVisible(true);

      one=new Thread(){
            public void run() {
                one.start();
                try{
                    System.out.println("Does it work?");
                    Thread.sleep(1000);
                    System.out.println("Nope, it doesnt...again.");
                }catch(InterruptedException v){System.out.println(v);}
                }  
               };
             }
share|improve this question
 
 
I can recommend the book Java Concurrency In Practice if you are new to threads but would like to know more. It's an enjoyable book and it takes you through threads beginning with the basic concepts. –  sbrattla Jul 20 at 5:24
 
this one would help you –  01000001 Jul 20 at 5:43

3 Answers

up vote 9 down vote accepted

you are calling the one.start() method in the run method of your Thread. But the run method will only be called when a thread is already started. so do this instead.

one = new Thread() {
    public void run() {
        try {
            System.out.println("Does it work?");

            Thread.sleep(1000);

            System.out.println("Nope, it doesnt...again.");
        } catch(InterruptedException v) {
            System.out.println(v);
        }
    }  
};

one.start();
share|improve this answer
 
Helped a lot, thank you! –  Matt Martin Jul 20 at 3:47
 
@user2556777 my pleasure :) –  StinePike Jul 20 at 3:49

You need to do two things:

  • Start the thread
  • Wait for the thread to finish (die) before proceeding

ie

one.start();
one.join();

If you don't start() it, nothing will happen - creating a Thread doesn't execute it.

If you don't join) it, your main thread may finish and exit and the whole program exit before the other thread has been scheduled to execute. It's indeterminate whether it runs or not if you don't join it. The new thread may usually run, but may sometimes not run. Better to be certain.

share|improve this answer
 
Starting one thread and joining it immediately is the exception rather than the rule. If we always do this, we would never have more than two threads running at a time and one of them would be waiting in the join(). If what you meant was more along the lines of 'join the thread at the point in the code where you need it to have its work completed' then I would agree with the recommendation. –  Rob Jul 20 at 4:12
 
@Rob You would not normally join a thread, but in this case he's calling it from his main() which then (appears to) do nothing else, so it may well exit "immediately", and when the main thread exits the JVM shuts down - all (non-daemon) threads are killed and those not yet executed will never execute. It was only recommending join() because of the particular situation. –  Bohemian Jul 20 at 4:29

If you want more Thread to be created, in above case you have to repeat the code inside run method or at least repeat calling some method inside.

Try this, which will help you to call as many times you needed. It will be helpful when you need to execute your run more then once and from many place.

class A extends Thread {
    public void run() {
             //Code you want to get executed seperately then main thread.       
    }
     }

Main class

A obj1 = new A();
obj1.start();

A obj2 = new A();
obj2.start();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.