This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Threads: join() and volatile instance variables

 
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
MyEclipse IDE Firefox Browser Linux
  • 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
MyEclipse IDE Firefox Browser Linux
  • 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: 12422
269
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic