• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Threads: join() and volatile instance variables

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic