• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Threads: join() and volatile instance variables  RSS feed

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone please tell me that what is difference when we use volatile instance variables in threads and without voitaile keyword in instance variables in thread. Also how is it possible to call join() function directly without using thread object i.e when Thread t=new Thread(); we should call like this: t.join(). But in the code we can directly call join(). How will I identify that which thread will join to which if I simply call join() function.

Thanks,
Saumya
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volatile variables are fetched from main memory and are not cached anywhere within the thread. Trips to main memory are expensive

But in the code we can directly call join()



t.join() tells the JVM, 'Please wait for thread t to die and then continue execution'. The aim being that one thread should wait for another to complete before proceeding. join() calls the method on the current executing instance. I am not sure what that would accomplish
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code which is from examlab. How am I getting output: 000000 ? I didn't get

 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last instance initializes the static variable to 0. Thus you get 0 all the time



Try doing this instead



the result will be different
 
Saloon Keeper
Posts: 10234
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that x is declared static.

Also regarding your question on volatile. If a variable is volatile, it simply means that threads shouldn't cache intermediary results from operations on that variable. Caching makes these operations a lot faster, but when you're working with multiple threads, this means that different threads won't see the effects of an operation by another thread on a variable. To counteract this, you declare the variable volatile, so all threads can see operations on that variable by other threads.
 
Happily living in the valley of the dried frogs with a few tiny ads.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!