aspose file tools*
The moose likes Beginning Java and the fly likes Confused With Multi Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused With Multi Threading" Watch "Confused With Multi Threading" New topic
Author

Confused With Multi Threading

RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18
I am trying with a basic program of multi threading . It worked as desired when i run it in Windows. Now i am using UBUNTU but i don't understand the output now The following the basic code i am using .


This is the out put i could find . Even on running multiple times . I tried to find the Thread priority which says it is 5 in both the threads . But still i dont find random output.
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method

.

Can anyone please explain me about this behavior in UBUNTU
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
What are you expecting to happen?

There is no guarantee that, just because another thread is defined, that it will run. If there is a reason to force one thread to run instead of another, then you need to use wait and notify and tell the program what you want it to do. But if all you do is start two threads, it is quite within the bounds of thread definition to run one to completion and then run the other one.

It is also acceptable for it to do different things for different runs.

Perhaps your Ubuntu implementation takes a while to set up a thread compared to how long it takes to execute 10 trace statements, and therefore you should expect it to behave this way.

rc
RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18
So can i understand that my ubuntu is taking very less time than that of windows, as all together the looped statements are running together, Because i have tried it running several times but still i dont find the random output which i usually find in Windows evironment.

Even i find the same output even when i try implementing Runnable interface instead of extending thread class. I know there wont be much change in either of these methods but i just wanna convey the behavior
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Make you code a little more interesting by simulating a longer running task in each of your loops:


Try sleeping for different lengths of times in each loop.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I like Rob's idea. If you want to take it a step further, generate a random number to wait between loop executions.
RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18
I tried using sleep() method but there is not much change , i could find the output that i expected ,i.e as follows

IN the Main thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the My thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method
IN the Main thread method

But the changes that i made in the code is only i added sleep() method in the main method i couldn add to run method if i do so i find the following error .

ThreadDemo.java:4: run() in Mythread cannot implement run() in java.lang.Runnable; overridden method does not throw java.lang.InterruptedException
public void run() throws InterruptedException


Can anyone help me out on how to use sleep method in the run() method
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Add a try/catch block to your code.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I changed the class names...
RajTilak Sivaluri
Greenhorn

Joined: Nov 13, 2010
Posts: 18
Kudos Rob . your idea worked and i got the output (Random output ) . Thank [b]you Ralph for your help.[/b]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused With Multi Threading