Execution of Multiple Threads in Java

Ads
 

Execution of Multiple Threads in Java

Can anyone tell me how multiple threads get executed in java??I mean to say that after having called the start method,the run is also invoked, right??Now in my main method if I want to instantiate more than two instances of the same class which extends Thread,how and when will each of the child thread enter the run() and get executed??

class NewThread extends Thread {

NewThread() {

  // Create a new, second thread                                      
  super("Demo Thread");
  System.out.println("Child thread: " + this);
  start(); // Start the thread

}

// This is the entry point for the second thread.

public void run() {
try { for(int i = 5; i > 0; i--) {

        System.out.println("Child Thread: " + i);                                    
        // Let the thread sleep for a while.
        Thread.sleep(500);
     }
  } catch (InterruptedException e) {
     System.out.println("Child interrupted.");
  }
  System.out.println("Exiting child thread.");

} }

class ExtendThread {

public static void main(String args[]) {

  new NewThread(); // create a new thread
  try {
     for(int i = 5; i > 0; i--) {
        System.out.println("Main Thread: " + i);
        Thread.sleep(1000);
     }
  } catch (InterruptedException e) {
     System.out.println("Main thread interrupted.");
  }
  System.out.println("Main thread exiting.");

} }

If inside the ExtendThread class if I add another statement "new NewThread()" the first child thread after being created enters the run() along with the second child thread which also enters the run and both of them executes at the same time.But isn't it this way, the first child thread after being created is supposed to execute completely and only then the second child comes into the scene and executes.

View Answers

Ads









Related Tutorials/Questions & Answers:
Execution of Multiple Threads in Java
Execution of Multiple Threads in Java  Can anyone tell me how multiple threads get executed in java??I mean to say that after having called the start method,the run is also invoked, right??Now in my main method if I want
Time schedular for multiple batch file execution in java
Time schedular for multiple batch file execution in java  Hello All, i have a task in which i have to design a time schedular in java in which i can automate the batch file execution for the wholw day. For example i should have
Advertisements
Creating multiple Threads
In this section you will learn how to create multiple thread in java. Thread is a path of execution within a program, it is a part of process. JVM allow multiple thread to run concurrently. Each and every thread has the priority
Java Multithreading
. In Java, the Java Virtual Machine (JVM) allows an application to have multiple threads of execution running concurrently independently. When a program contains multiple threads then the CPU can switch between the two threads to execute them
threads in java
threads in java  how to read a file in java , split it and write into two different files using threads such that thread is running twice
threads in java
threads in java  iam getting that the local variable is never read in eclipse in main classas:: class Synex4{ public static void main(String args[]){ Test1 ob1=new Test1(); //local variable never read
Java Threads - Java Beginners
Java Threads  Why we use synchronized() method?  Hi Friend... allows the threads to wait for resources to become available and also notify the thread that makes resource available to notify other threads
Java - Threads in Java
Java - Threads in Java       Thread is the feature of mostly languages including Java. Threads... or multiprogramming is delivered through the running of multiple threads concurrently
Multithreading in Java
Multithreading in java is running multiple threads sharing same address space... to be more responsible to the user. When a program contains multiple threads... simultaneously. Each part is called a thread and has a separate path of execution
Multithreading Example In Java
of execution i.e. a Java application can contain the concurrently executable multiple... multithreading in Java. Multithreading in Java is used to execute multiple tasks... threads to execute them concurrently. In multithreading in Java some of the non
execution of java program
execution of java program  wat are the steps for the execution of the servlet-jdbc program on tomcat 5.5 server.. PlS provide..the execution steps in detail..including the setting of classpath in DOS for mysql-connector-java. i
Threads in Java Swing MVC Application
Threads in Java Swing MVC Application  Hello, I am currently making a Java Swing application, but I am having a lot of trouble with implementing threads into my program. I use the MVC paradigm and I just can't seem to implement
java: try finally blocks execution
java: try finally blocks execution  java: try finally blocks execution
Threads in Java
Threads in Java help in multitasking. They can stop or suspend a specific... and allows other threads to execute. Example of Threads in Java: public class... in increasing the speed of the processes. In Java programming, Java Virtual Machine (JVM
Java Execution Time Measurement(JETM)
Java Execution Time Measurement(JETM)  How can we integrate JETM in eclipse
Java Execution Time Measurement(JETM)
Java Execution Time Measurement(JETM)  Whether JETM can be implemented in Struts framework especially in Struts2. If not,tell the alternative solution
threads
threads  what are threads? what is the use in progarmming
Synchronized Threads
; In Java, the threads are executed independently to each other. These types... being corrupted by multiple threads by a keyword synchronized to synchronize them... methods, multiple threads can still access the class's non-synchronized methods
Synchronized Threads
; In Java, the threads are executed independently to each other. These types... being corrupted by multiple threads by a keyword synchronized to synchronize them...-synchronized methods, multiple threads can still access the class's non
Storing Multiple image in sql using java
Storing Multiple image in sql using java  Hi, How to store and retrieve a multiple image in sql using java but already i have created sql table if i want to insert a image while runtime execution. Can anyone tell me solution
insert multiple selection - Java
insert multiple selection - Java  how to insert multiple selection values from html into database using servlets
Does Java support multiple Inheritance?
Does Java support multiple Inheritance?  Hi, I am beginner in Java and trying to implement multiple inheritance in Java. But could not find any example. Does Java support multiple Inheritance? Thanks   Hi, Good
Does Java support multiple Inheritance?
Does Java support multiple Inheritance?  Hi, I am beginner in Java and trying to implement multiple inheritance in Java. But could not find any example. Does Java support multiple Inheritance? Thanks   Hi, Good
Does Java support multiple Inheritance?
Does Java support multiple Inheritance?  Hi, I am beginner in Java and trying to implement multiple inheritance in Java. But could not find any example. Does Java support multiple Inheritance? Thanks   Hi, Good
pls tell me the difference between the run() and start() in threads in java....
pls tell me the difference between the run() and start() in threads in java....  difference between the run() and start() in threads in java
Threads
)Separates the code from execution 3)Allows you to run your runnable from a Thread Pool
Does Java support multiple inheritance?
Does Java support multiple inheritance?  Hi, Does Java support multiple inheritance? thanks   hi, Please visit the following link: http://www.roseindia.net/java/language/inheritance.shtml Hope
Java example to get the execution path
Java example to get the execution path       get execution path We can get the execution path of the system in java by using the system property. For getting execution
Explain about threads:how to start program in threads?
; Learn Threads   Thread is a path of execution of a program... and print it simultaneously. Threads are called light weight processes. Every java...Explain about threads:how to start program in threads?  import
implementing an algorithm using multi threads - Java Beginners
to breakdown into two or three threads and need to implemented and need... endtime = System.currentTimeMillis(); System.out.println("Total execution
Display Multiple Images in jscrollpane using Java Jpanel
Display Multiple Images in jscrollpane using Java Jpanel  Browse and Display multiple images in vertical view of java jscrollpane using jpanel

Ads