Results 1 to 7 of 7
  1. #1
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default problem in multi threads execution

    actually i am trying to perform multi threading on two different thread classes. but after executing everytime its showing different output. could anyone tell me why its showing like this:
    class MyThread extends Thread{
    public void run(){
    try{
    for (int i = 0; i < 10; i++) {
    Thread.sleep(1000);
    System.out.println("i"+i);

    }
    }catch (Exception e) {
    // TODO: handle exception
    }
    }

    }
    class MyThread1 extends Thread{
    public void run(){
    try{
    for (int j = 0; j < 10; j++) {
    Thread.sleep(1000);
    System.out.println("j"+j);

    }
    }catch (Exception e) {
    // TODO: handle exception
    }
    }

    }
    public class MultiThread {
    public static void main(String[] args) {
    MyThread m=new MyThread();
    MyThread1 m1=new MyThread1();
    Thread t=new Thread(m);
    Thread t1=new Thread(m1);
    t.start();
    t1.start();
    }

    }


    outputs i am getting:
    first time execution:
    i0 j0 i1 j1 i2 j2 i3 j3 i4 j4 i5 j5 i6 j6 i7 j7 i8 j8 i9 j9

    Second time execution:
    j0 i0 j1 i1 i2 j2 i3 j3 j4 i4 j5 i5 j6 i6 i7 j7 j8 i8 j9 i9

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: problem in multi threads execution

    What would you expect?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: problem in multi threads execution

    This is normal behavior when running multiple threads. They run asynchronously, thus it is non-deterministic when each print statement will be reached.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default Re: problem in multi threads execution

    i am expected the first result. but after executing next time its giving another result. i am not getting why its giving like that

  5. #5
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default Re: problem in multi threads execution

    so you are telling, because of asynchronous behavior its displaying like that

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: problem in multi threads execution

    Yes. The scheduler determines when each thread will run. In a muti-processor environment, threads may actually run concurrently if there is one thread per processor. But threads are asynchronous entities. Some of that can be controlled with synchronization. But that is to ensure that threads don't interfere with each other, not necessarily to ensure that they are scheduled at the same time. Here is a simple example. Imagine that you gave each of two helpers a bucket and said you needed 20 buckets of water. In this case you really don't care in what order they return with their respective buckets of water. What you do care about is that the task was done in half the time that one helper could do it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: problem in multi threads execution

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Deterministic execution of Java Threads, tool support
    By pulkittomar in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-16-2011, 05:30 AM
  2. Serial Execution of threads
    By gamodg in forum Advanced Java
    Replies: 4
    Last Post: 11-09-2010, 05:37 PM
  3. execution of threads
    By jyotigupta6 in forum Threads and Synchronization
    Replies: 5
    Last Post: 11-16-2008, 12:28 AM
  4. Threads execution stop
    By jyotigupta6 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-31-2008, 01:41 PM
  5. slow execution and flickery graphics. too many threads?
    By lost_in_java in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-23-2008, 02:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •