Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Multithreading

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multithreading

    Please explain the flow of execution of this code...


    // Create multiple threads
    class NewThread implements Runnable {
    String name; // name of thread
    Thread t;
    NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
    }
    // This is the entry point for thread.
    public void run() {
    try {
    for(int i = 5; i > 0; i--) {
    System.out.println(name + ": " + i);
    Thread.sleep(1000);
    }
    } catch (InterruptedException e) {
    System.out.println(name + "Interrupted");
    }
    System.out.println(name + " exiting.");
    }
    }




    class MultiThreadDemo {
    public static void main(String args[]) {
    new NewThread("One"); // start threads
    new NewThread("Two");
    new NewThread("Three");

    try {
    // wait for other threads to end
    Thread.sleep(10000);
    } catch (InterruptedException e) {
    System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Multithreading

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Please ask a specific question, not for an explanation that could go on for days. After you post the code correctly, you might run the code and then explain how you think the code should work and/or describe how what you thought would happen is not the same as what actually happens and ask why.

    --- Update ---

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Please ask a specific question, not for an explanation that could go on for days. After you post the code correctly, you might run the code and then explain how you think the code should work and/or describe how what you thought would happen is not the same as what actually happens and ask why.

Similar Threads

  1. Multithreading question
    By Hikaros in forum Java Theory & Questions
    Replies: 1
    Last Post: November 23rd, 2013, 01:49 AM
  2. Complicated Multithreading
    By didingnuriska in forum Threads
    Replies: 16
    Last Post: April 19th, 2013, 02:17 PM
  3. Multithreading
    By Miths in forum Java Theory & Questions
    Replies: 10
    Last Post: December 17th, 2012, 02:46 AM
  4. Multithreading Problem
    By Staticity in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 2nd, 2012, 07:39 PM
  5. Multithreading in java
    By skillet in forum Java Theory & Questions
    Replies: 1
    Last Post: March 17th, 2012, 07:32 AM