SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
SCJP 1.6 96%
Raju Champaklal wrote:dude you are creating a new object everytime in the loop.. five times
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Minhaj kaimkhani wrote:why its not printing Numbers 1 to 5 in sequence with "N Done" anywhere in the sequence where N cannot be determined?
Ulf Dittmer wrote:
Minhaj kaimkhani wrote:why its not printing Numbers 1 to 5 in sequence with "N Done" anywhere in the sequence where N cannot be determined?
Is that not what it's doing? If not, what *are* you getting?
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
SCJP 6
SCJP 1.6 96%
Neha Daga wrote:yes, Raju is right.
you are creating a new thread object everytime the for loop runs and start it. but its jvm dependent that which thread will get into running state so whichever thread gets control first sequence depends on that, even if your run method is synchronized.
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
yes we are creating new object into loop every time, but the counter variable is static so it must be shared among all the objects
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Ulf Dittmer wrote:The code you just added does nothing at all, since the main method is single-threaded. The problem is not about synchronization in the main method, it's about synchronization in the run method.
Let's look at it the other way around: *If* you were to accept that synchronization is *not* happening, would you understand why you're getting the output that you're getting? If not, I suggest you work through the Sun Java Tutorial chapter on concurrency: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
SCJP 1.6 96%
Neha Daga wrote:ok..let me try to explain.
everytime when you create a new TestClass object in the for loop you start a new thread and each new thread has its own run method, so when each thread runs it will execute its run method and access threadcounter which is a class variable so when one thread is about to print threadcounter some other thread may have enter its run method and and changed the threadcounter it simply depends on which thread gets into running state when and synchronising run method here will not do any synchronization.
Neha Daga wrote:now if you look at Ulf's code he has synchronized the block of statements which include threadcounter increment and its printing out making it whole as an atomic operation so no matter which threads come into running state when this block will be accessed by only one thread and now synchronization is perfect.
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Minhaj kaimkhani wrote:So, you mean that only 1 thread will execute at a time(this means valid synchronization too??)
Minhaj kaimkhani wrote:if only one thread run at a time so the chances to increment of threadcounter obviously 1 at a time!
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |