Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By j2me64

Thread: What is wrong with my code?

  1. #1
    tweetychirpy is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default What is wrong with my code?

    This is an array of Thread in Worker class that implements Runnable. i donno why the threads are not starting :(

    import java.util.*;

    public class Worker extends Thread {

    public static void main(String[] args) {
    Worker[] worker = new Worker[10];
    Thread[] thread = new Thread[10] ;
    for (int i = 0 ; i < thread.length ; i++){
    thread[i] = new Thread(worker[i]);
    thread[i].start();
    }

    }

    public void run(){
    System.out.println("hello");
    }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: What is wrong with my code?

    Quote Originally Posted by tweetychirpy View Post
    This is an array of Thread in Worker class that implements Runnable. i donno why the threads are not starting :(
    Not true! The class Worker extends a Thread and not the interface Runnable!

    BTW: One instance of a class that implements Runnable can by passed to multiple threads! So the array of Workers is not needed, but only one reference. Last error: all worker[i] are null!
    Last edited by j2me64; 12-23-2011 at 05:13 PM.

  3. #3
    tweetychirpy is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: What is wrong with my code?

    Apologies!!



    import java.util.*;

    public class Worker implements Runnable {

    public static void main(String[] args) {
    Worker[] worker = new Worker[10];
    Thread[] thread = new Thread[10] ;
    for (int i = 0 ; i < thread.length ; i++){
    thread[i] = new Thread(worker[i]);
    thread[i].start();
    }

    }

    public void run(){
    System.out.println("hello");
    }
    }

  4. #4
    tweetychirpy is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: What is wrong with my code?

    Thanks got the issue...its working nw :)

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: What is wrong with my code?

    You don't need to have an array of size 10 for the runnable Worker class, only one is enough

    Java Code:
    	public static void main(String[] args) {
    		Worker worker = new Worker();
    		Thread[] thread = new Thread[10];
    		for (int i = 0; i < thread.length; i++) {
    			thread[i] = new Thread(worker);
    			thread[i].start();
    		}
    	}
    It's true that the same thread can be started only once, but the same runnable can passed to several threads.
    Last edited by j2me64; 12-24-2011 at 01:21 PM.
    DarrylBurke likes this.

Similar Threads

  1. What's wrong with my code?
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 10-30-2011, 01:38 PM
  2. what is wrong with my code :(
    By lexyloraine in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 05:37 AM
  3. What is wrong with my code?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 11:44 PM
  4. What's wrong with my code?
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 07:00 PM
  5. what is wrong with my code???
    By EBBOOO in forum New To Java
    Replies: 30
    Last Post: 07-27-2010, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •