Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-21-2004, 12:25 AM   #1
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
java thread trouble

i am having trouble getting this to work... shouldnt the output from this code do everything at once, example have output like this

Thread One running, iteration: 1
Thread Two running, iteration: 1
Thread Three running, iteration: 1
Thread Two running, iteration: 2


class DemoThread
     public static void main(String args[]) 
         String lockOut = "This variable provides mutual exclusion.";

         System.out.println("MAIN Thread: Create 3 threads.)");
         ChildProcess thread1 = new ChildProcess("One  ",lockOut);
         ChildProcess thread2 = new ChildProcess("Two  ",lockOut);
         ChildProcess thread3 = new ChildProcess("Three",lockOut);

         System.out.println("MAIN Thread: Run the 3 threads.)");

             System.out.println("MAIN Thread: Waited for 3 threads to finish.");
         catch (InterruptedException e) 
             System.out.println("Error: MAIN Thread Interrupted.");

         System.out.println("MAIN Thread: Done, Terminating.");
    }// End Main

} //End DemoThread

class ChildProcess extends Thread
    String pName, lock;

    public ChildProcess(String name, String lockOut)
        pName = name;
        lock  = lockOut;

    public void run() 
       synchronized (lock)
          System.out.println("     STARTING Thread " + pName);
          for(int i=1; i<=4; i++)  
              System.out.println("     Thread " + pName + 
                                 " running, iteration: " + i);

          catch (InterruptedException e) 
              System.out.println("      Error: " + pName + " interrupted.");

          for(int i=5; i<=8; i++)  
              System.out.println("     Thread " + pName + 
                                 " running, iteration: " + i);

          System.out.println("     EXITING Thread " + pName);

} //End ChildProcess
current output...

MAIN Thread: Create 3 threads.)
MAIN Thread: Run the 3 threads.)
Thread One running, iteration: 1
Thread One running, iteration: 2
Thread One running, iteration: 3
Thread One running, iteration: 4
Thread One running, iteration: 5
Thread One running, iteration: 6
Thread One running, iteration: 7
Thread One running, iteration: 8
EXITING Thread One
STARTING Thread Three
Thread Three running, iteration: 1
Thread Three running, iteration: 2
Thread Three running, iteration: 3
Thread Three running, iteration: 4
Thread Three running, iteration: 5
Thread Three running, iteration: 6
Thread Three running, iteration: 7
Thread Three running, iteration: 8
EXITING Thread Three
Thread Two running, iteration: 1
Thread Two running, iteration: 2
Thread Two running, iteration: 3
Thread Two running, iteration: 4
Thread Two running, iteration: 5
Thread Two running, iteration: 6
Thread Two running, iteration: 7
Thread Two running, iteration: 8
EXITING Thread Two
MAIN Thread: Waited for 3 threads to finish.
MAIN Thread: Done, Terminating.
Old 09-21-2004, 04:40 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
The system waits for a certain amount of time between switching each thread.

The exact numbers depend on your Java IDE (and your system's schedular if you're using native threads), but each thread will typically run for longer than the time needed to enqueue eight lines of text in the output buffer.

To achieve the desired effect, you will have to enable the Thread.sleep() call (and its try/catch) and possibly move it inside the for(i) loop, or run for many more iterations (probably a few hundred or more).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run Bash scripts like thread in Java? nguyenzoro Linux - General 1 11-11-2005 03:08 AM
monitor java thread usage against o/s limits rlangsto Linux - General 1 02-11-2005 01:20 PM
another 5 button mouse trouble thread sheepdogj15 *BSD 11 01-07-2005 01:30 PM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 04:47 AM
A Java Thread question megaspaz Programming 0 08-02-2003 02:33 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration