• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Many Threads

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestClass extends Thread
{
private static int threadcounter = 0;
public void run()
{
threadcounter++;
System.out.println(threadcounter);
}
public static void main(String[] args) throws Exception
{
for(int i=0; i<10; i++)
{
synchronized(TestClass.class)
{
new TestClass().start();
}
}
}
}

In a mock exam the "correct" says that 10 will be the final number printed. I do think this is necessarily so, because 2 threads may update threadcounter simultaneously, and the final number may be less than 10.

What about it you Thread experts.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the method is Synchronized only one thread can access it.

I tried the code. It printed 1 to 10.

Thanks
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The synchronization is a misleading as the answer in the Mock makes clear. As soon as start() is called on a thread it is no longer synchronized.
 
author
Posts: 23958
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
Yes... This example is definitely *not* threadsafe. And in theory, it should be possible for two threads to postfix increment at the same time, and only incrementing it once.

Henry
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.

I was looking for an answer from you when I put this up.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On almost all modern machines, the post fix increment operator is, on the machine language level, implemented as an atomic operation.

So if the machine is a single processor machine, I don't believe synchonization would be needed in case it is just a word size primitive that is being incremented. The underlying hardware guarantees that operations on the primitive would be atomic.

If a multicore processor is being used, more complex memory consistency issues may come into play and probably lead to unpredictable results.

Of course, one doesn't ever want to depend on the underlying hardware when programming with Java. So the correct way is, like you already know, to use synchronization when modifying the shared variable.
 
Henry Wong
author
Posts: 23958
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
The JVM specification actually goes and defines what types / operations are atomic.

Basically, every load and store (of everything but long and double) is atomic. If the variable is declared as volatile, then not only will it not cache the variable, but long and double will also be atomic.

The increment operators are not defined as atomic.

On almost all modern machines, the post fix increment operator is, on the machine language level, implemented as an atomic operation.



Keep in mind, that there are 2 machines here. The javac compiler will compile to the Java Virtual Machine, and the JIT compiler will compile it native machine. The Java Virtual Machines does not have an increment operator.

It's a bit more than just "depend on the underlying hardware", you are also depending on the JVM/JIT compiler that your users has installed.

Henry
 
Oscar Johansson
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the interesting info Henry.

I did assume that Java code would be JIT compiled optimally for "modern machines".
 
Always look on the bright side of life. At least this ad is really tiny:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic