Results 1 to 5 of 5
Thread: What is wrong with my code?
- 12-23-2011, 04:08 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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");
}
}
- 12-23-2011, 05:05 PM #2
Re: What is wrong with my code?
Last edited by j2me64; 12-23-2011 at 05:13 PM.
- 12-23-2011, 05:12 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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");
}
}
- 12-23-2011, 05:23 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: What is wrong with my code?
Thanks got the issue...its working nw :)
- 12-23-2011, 06:16 PM #5
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(); } }
Last edited by j2me64; 12-24-2011 at 01:21 PM.
Similar Threads
-
What's wrong with my code?
By Johanis in forum New To JavaReplies: 1Last Post: 10-30-2011, 01:38 PM -
what is wrong with my code :(
By lexyloraine in forum New To JavaReplies: 2Last Post: 03-23-2011, 05:37 AM -
What is wrong with my code?
By Solarsonic in forum New To JavaReplies: 3Last Post: 03-22-2011, 11:44 PM -
What's wrong with my code?
By Isong in forum AWT / SwingReplies: 1Last Post: 11-16-2010, 07:00 PM -
what is wrong with my code???
By EBBOOO in forum New To JavaReplies: 30Last Post: 07-27-2010, 10:59 PM
Bookmarks