• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused With Multi Threading

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add a try/catch block to your code.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the class names...
 
RajTilak Sivaluri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kudos Rob . your idea worked and i got the output (Random output ) . Thank [b]you Ralph for your help.[/b]
 
reply
    Bookmark Topic Watch Topic
  • New Topic