Thread concept

Ads
 

Thread concept

Everytime when i run a multithread program it gives different output: my means of executing the threads in various manners here i have one, two , three threads. At one time it runs one, two , three and on other it runs two, three, one and so on why this happens? can anyone help me thx in advance friends. Happy new year!!!!!

class Newthread3 implements Runnable{
Thread t;
String name;
Newthread3(String threadname){
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: "+t);
t.start();

}
public void run(){
try{
t.sleep(1000);
for(int i=5;i>0;i--){
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println("Interrupted");
}
System.out.println(name+" exiting");
}
}

class Demojoin{
public static void main(String a[]){
Newthread3 ob1=new Newthread3("One");
Newthread3 ob2=new Newthread3("Two");
Newthread3 ob3=new Newthread3("Three");
System.out.println("Thread One is alive: "+ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ob3.t.isAlive());
try{
System.out.println("Wait for other threads to join");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e){
System.out.println("Interrupted");
}
System.out.println("Thread One is alive: "+ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ob3.t.isAlive());
System.out.println("Main Thread Exiting");
}
}
View Answers

Ads









Related Tutorials/Questions & Answers:
Thread concept
Thread concept  Everytime when i run a multithread program it gives... in advance friends. Happy new year!!!!! class Newthread3 implements Runnable{ Thread t; String name; Newthread3(String threadname){ name=threadname; t=new Thread
Explain the concept of thread priorities in Java.
Explain the concept of thread priorities in Java.  Explain the concept of thread priorities in Java
Advertisements
Java :Thread Synchronization
Java :Thread Synchronization This section explains how to use concept of synchronization in java Thread. Thread Synchronization : . Java supports multi-threading concept that is multiple threads run parallel to complete the execution
Thread
Thread  Thread Life Cycle
Thread
Thread  what is the use of thread
Thread
Thread  Explain two ways of creating thread in java. Explain at three methods of thread class.   Java Create Thread There are two main ways of creating a thread. The first is to extend the Thread class and the second
Thread
Thread  What is multi-threading? Explain different states of a thread... processor system. States of Thread: New state ? After the creations of Thread instance the thread is in this state but before the start() method invocation
Thread
to the thread constructor eventhough we had created only one thread and if you say we have added to point to the current thread then why we have not added this in the following line "s=s1" Pls reply...... class MyThread extends Thread { Thread
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
Thread
Thread  class Extender extends Thread { Extender(Runnable run...(); } public void run(){ System.out.println("Extender Thread is Started :"); //new Thread(new Implementer()).start(); } } class Implementer
Thread
Thread  will this code work..? class A extends Thread { public...=" + i); } public static void main(string args[]) { A a = new A(); Thread t = new thread(a); t.start(); } } Is it possible to run above program with out
Thread
Thread  Write a Java program to create three theads. Each thread should produce the sum of 1 to 10, 11 to 20 and 21to 30 respectively. Main thread....   Java Thread Example class ThreadExample{ static int
Thread
Thread   there are two threads running at a time.. when am updating a values in database. both thread halt and stop for moment till it get updated into database... so i dnt want thread to get halts for tht moment of period. whats
How to Differenciate Main Thread and Child Thread in Java
. The Below reference will provides you clear idea of java thread concept. http...How to Differenciate Main Thread and Child Thread in Java  hi... Main Thread and Java Child Thread. Please Suggest any example or online link
Java :Thread getPriority Example
Java :Thread getPriority Example In this tutorial you will learn how to get thread priority in java thread. Thread getPriority() : Thread scheduler uses thread priority concept to assign priority to the thread. A higher priority 
Java :Thread setPriority Example
Java :Thread setPriority Example In this tutorial you will learn how to set thread priority in java thread. Thread setPriority() : Thread scheduler uses thread priority concept to assign priority to the thread. A higher priority 
Daemon thread
Daemon thread  what is need of daemon thread
Daemon thread
Daemon thread  what is need of daemon thread
java Thread
java Thread  what is purpose of Thread
Demon thread
Demon thread  What is demon thread? why we need Demon thread?  ... there are daemon thread by killing them abruptly.Any thread can be a daemon thread.../thread/daemon-threads.shtml
Concept of subqueires using JSP
Concept of subqueires using JSP  Can I implement the concept of sub queries in JSP page , if yes please tell me how this can be achieved. I have requirement where output of the SQL query should be given as input to the update
Java Thread
Java Thread       A java thread is an execution context or a lightweight process. It is a single sequential flow of control within a program. Programmer may use java thread mechanism
Create Thread by Extending Thread
Create Thread by Extending Thread This section explain how to create thread by extending Thread class in java. Extending Thread : You can create thread by extending Thread class and then by creating instance of that class you can
Thread in Nutshell
Thread in Nutshell  Hi, I m confused about what is called a thread actually in a program. There are many answer to this question on the web...; Please go through the following link: Thread Tutorials
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
Java Inheritance Concept.
Java Inheritance Concept.  Yesterday i got confused with following question in an aptitude exam. Question:Class A,B,C have method named doit(). Class B extends Class A,Class C extends Class B.How will you call method doit
Java :Thread dumpStack
Java :Thread dumpStack In this tutorial you will learn about Java Thread dumpStack . Thread dumpStack : JVM gives the concept of Thread Dump which... are created. When you create any thread ,it doesn't mean that it is actually
thread dump
thread dump  Hi, I wanted to understand the Locked/waiting state below in the java thread dump. Is it normal to have waiting on locked object monitor value to be same, in the snippet below both are<0x701dd1e0> i see a lot
Exception in thread
Exception in thread   Hi, In my code following error is coming: Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject I have imported: import org.json.JSONObject; but its not working. What
Exception in thread
Exception in thread   Hi, In my code following error is coming: Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject I have imported: import org.json.JSONObject; but its not working. What

Ads