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.
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
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