Problem in Threading , ThreadGroup
Class number 1
Code :
class PrintNameThread extends Thread
{
public PrintNameThread(String name) {
super(name);
}
public void run(){
String name = super.getName();
for(int x = 0 ; x < 10 ; x++)
{
System.out.println(name);
}
}
}
Class number 2
Code :
class PrintNameThreadv2 extends Thread
{
public PrintNameThreadv2(String name) {
super(name);
super.setPriority(Thread.MAX_PRIORITY);
// super.setDaemon(true);
super.start();
}
public void run(){
String name = super.getName();
for(int x = 0 ; x < 10 ; x++)
{
System.out.println(name);
}
}
}
Class number 3
Code :
class PrintNameThreadv3 implements Runnable
{
String name ;
public PrintNameThreadv3(String name) {
this.name = name ;
}
public void run(){
for(int x = 0 ; x < 10 ; x++)
{
System.out.println(this.name);
}
}
}
Main Class
Code :
public class ThreadGroupDemo01 {
public static void main(String args[])
{
PrintNameThread t1 = new PrintNameThread("System");
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
PrintNameThreadv2 t2 = new PrintNameThreadv2("Access");
PrintNameThreadv3 t3 = new PrintNameThreadv3("NEO");
Thread th = new Thread(t3);
th.start();
//Define an array to store current threads.
Thread threadArray[];
//Return current thread-group
ThreadGroup tg = Thread.currentThread().getThreadGroup();
int num = tg.activeCount();
System.out.println("Total running thread are " + num);
threadArray = new Thread[num];
tg.enumerate(threadArray);
for(int i = 0 ; i < num ; i++)
{
System.out.println("Thread name : " + threadArray[i].getName() + " , " +
"Thread priority : " + threadArray[i].getPriority()+ " , " +
"Group name : " + threadArray[i].getThreadGroup().getName());
}
/**/
}
}
I want to ask about why it's print 2 of current running threads.It's have to be 4
System,Access,Neo,Main
There is something wrong i didn't knew it...So if any one have can say something about this
or i misunderstand something.
Re: Problem in Threading , ThreadGroup
does your output look something like this?
Quote:
System
Access
Total running thread are 2
Thread name : main , Thread priority : 5 , Group name : main
Thread name : Thread-0 , Thread priority : 5 , Group name : main
NEO
I'll explain my output and hopefully you can understand what's going on with your output:
The first two lines are being printed out first because once these threads have been started, they'll run basically independent of the other threads (their data access may not be independent, but their runtimes are). They reached their print-out statements before the main thread could print out the "Thread name : "... stuff
The active count has dropped to 2 by the time the active thread number method was called because the first 2 threads have already stopped (reached the end of their run methods).
NEO was printed last because it's print statement happened to came after the thread info was printed.
Timing multi-threaded applications is a huge problem, and to be quite honest, i'm not too good at it. I have only basic knowledge of how multi-threading and parallel processing works (less than basic knowledge sometimes).