• 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

Thread doubt

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read from k&b book.
When it comes to threads, very little is guaranteed.
if the thread programs gives output differently everytime.how we can use it in real programing.then what is use of having Thread class in Java.
please explain me .
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No surprise that ,Java Thread is platform DEPENDENT,,
it means it depends on your os,cpu....,
different OSs use different algorothims in memory,like round robin,time slicing....etc.....
So the JVM does not know whatis the OS,Algorithim,CPU state....you are current working,,therefore this is not guaranteed,,,,,
you can GUARANTEE : if the thread run its new stack,it will complete.....
bye
 
vijay Thiru
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my problem is
i have run the thread program in the same system twice ,but it gives different output.
but i excepted same result. why it is working like this.

another one is
whenever Thread calls start method new Stack will be created.
is it correct or not.
if my understanding is wrong please correct me.
 
Alan Hermin
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(PLZ show us the code,so that i can discuss it)..
BUT Java specification says that The behavior in Threads is not guranteed.

whenever Thread calls start method new Stack will be created.


This is true.......
[ May 01, 2006: Message edited by: alaa hassan ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vijay Thiru:
read from k&b book.
When it comes to threads, very little is guaranteed.
if the thread programs gives output differently everytime.how we can use it in real programing.then what is use of having Thread class in Java.
please explain me .

 
srinivas murthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vijay Thiru:
read from k&b book.
When it comes to threads, very little is guaranteed.
if the thread programs gives output differently everytime.how we can use it in real programing.then what is use of having Thread class in Java.
please explain me .



We have methods like join(),wait(),notify() etc...which will give a specific behaviour to the threads....in this way it can be used in real programming.....
for example....if you use join()..it is guaranteed that one thread is run after the other.....
it is clearly mentioned in K&B book....
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if you join() two thread waiting for one object you can not guarantee wich will get it if they have the same priority
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer... Threads in Java have been used in "real programming" -- realtime applications, commercial products, software libraries, application servers, etc. -- for more than 10 years, so it is safe to assume it works...

Longer answer... Threads have always been somewhat non-deterministic. This is also true for Solaris threads, Windows threads, POSIX, etc. To manage this, developers generally...

1. Use synchronization techniques such as mutexes, semaphores, condition variables, reader-writer locks, message pipes, etc., to manage the flow of threads or the data they manage.

2. Understand the non-deterministic nature, and design applications to work under those conditions. (transactional techniques, optimistic locking techniques)

Unfortunately the learning curve can be somewhat steep. It can take years of experience before one can understand all the edge conditions for case one. And many programmers never even get to case two.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic