Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


siddharthan
Thread concept
0 Answer(s)      4 years and 3 months ago
Posted in : Java Beginners


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");
}
}

Advertisement
View Answers

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
thread
thread  can parent thread be dead if child thread is not dead
Thread
Thread  what is the use of thread
Thread
Thread  Thread Life Cycle
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  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  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  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
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
thread with in the servlet..? - JSP-Servlet
thread with in the servlet  Please explain me the concept of Java thread ..and it's use with Servlet.Thanks in advance
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
Thread - Java Beginners
Thread creation and use of threads in JAVA  Can anyone explain the concept of thread, thread creation and use of threads in JAVA application?  Thread creation and use of threads in JAVA Java Resourcehttp
Explain normalization concept?
Explain normalization concept?  Explain normalization concept
Thread in java
Thread in java  which method will defined in thread class
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 
Thread restart
Thread restart  hello,, can dead thread restart?   hii, A dead Thread can't restart not even by calling start() again on that object
java Thread
java Thread  what is purpose of Thread
Daemon thread
Daemon thread  what is need of daemon thread
Daemon thread
Daemon thread  what is need of daemon thread
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 
Java thread
Java thread  How can a dead thread be restarted?  A dead thread cannot be restarted
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
session concept - JSP-Servlet
session concept  Hello friends, How can we track unexpectedly closing a window when a jsp project running with session concept. And this tracking should update the log in status in data base
Java thread
Java thread  What are the high-level thread states
Java thread
Java thread  What are the ways in which you can instantiate a thread
Java thread
Java thread  What invokes a thread's run() method
 

 

 

DMCA.com