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

Thread lock doubt

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following is correct regarding the execution of the given program?


Select 2 awnsers:
a.Numbers 1 to 10 will be printed (none repeated or missed) in a serial order.
b.Numbers 1 to 10 will be printed (none repeated or missed) in any order.
c.Total of 10 numbers will be printed
d.The final value of threadcounter just before the program terminates will be 10.
e.The final value of threadcounter just before the program terminates can be any thing from 1 and 10.

NowI think a,c,d are correct,but it says select only 2 awnsers
[ September 18, 2005: Message edited by: anand phulwani ]
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c is correct not b as every object has its own lock,
method run is not synchronized and so a thread may increase value (assume from 0 to 1)
and now it stops and other thread starts and it may increase value (1 to 2)
now it will print and exit now first thread starts it prints same as last thread as variable is static only one copy.
both prints 2 (just example).

yes finally threadcounter will be 10.

please correct if required.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
This is the output of the program
----------------------------------
1
2
3
4
5
6
7
8
9
10
----------------------------------

It shows that option
a is correct: Numbers 1 to 10 will be printed (none repeated or missed) in a serial order.

b is correct: Total of 10 numbers will be printed

"The final value of threadcounter just before the program terminates will be 10" it seems that this is also true. and hence d is also correct.

Sorry but no explination.

Sandy
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sandeep output what you say is not always true,
threading is not guaranteed to work same way on all machine,
it is system dependent.
at one place threading may allow fair turn taking and
on other machine one thread may run to completion.

however in above case if you say
public synchronized void run()
or you use synchronize block and obtain lock on appropriate object than order can be guaranteed.
[ September 18, 2005: Message edited by: Santana Iyer ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put my money on the same horse as Santana
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

synchronized(TestClass.class)
Here we are having lock on object or on the class ?

i have just given the output what my computer showed me...and thus i conclueded.
even i wasnt sure of the answer therefore couldnt give any explinations.

I request you to run the code on your machine and tell me if you get a different output.

Thanks
Sandy
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep I get same output and most people will on most machines,
even if you run it many times.

But the point is it is not guaranteed, see exam expects us in case of threads what is and what is not guaranteed.

what you say about TestClass.class now this is class level lock
threads created new TestClass().start(); they all are different so
in all n new threads are started each object has its own lock.


see what we have in run method is just a print statement think when we have real logic much long and bigger than a simple statement.




just try one thing put System.out.print(threadcount);

now you will agree that main is also a thread and put the above statement just after for loop run program and you will notice that it may appear anywhere because same reason it is not guaranteed when main thread will run it may run before any of thread or may run last or in between.
[ September 18, 2005: Message edited by: Santana Iyer ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep , I ran the program and received the same values as you. The order of the values will never change because threadcounter is defined as static and all thread are incrementing this value and printing it. Therefore, regardless of which thread processes the threadcounter it will be greater then the previous thread.

However, thread racing is still occuring in the program. If you pause the thread for a couple of seconds between the threadcount++ and the Print statement you should receive values that are either duplicated or possible all 10. I did this by placing a for loop between the two statements. But, you could Use the the thread wait method with a specified time.


hope this helped
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Independently of Thomas I was modifying your code thus:




Here is the output. The call to the Thread sleep method causes the thread scheduler to give other threads a chance to run.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
But can anyone explain me what we are doing in this line:
synchronized(TestClass.class)
on what are we trying to obtain lock?

Thanxs
Sandy
[ September 19, 2005: Message edited by: Sandeep Chhabra ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The lock is that belonging to the Class object that represents the TestClass class itself. The same lock is used when you synchronize a static method.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still a little confused. If I wanted the threads to run sequentially and have unique numbers, what exactly would I change? The object that is being locked on?

Could someone give an example of code that would always (on any platform) print the numbers sequentially and each only once and contrast it to the code above?

Thanks,
Josh
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joshua, I did mention it previously mark run method synchronized
or else you can also do
t.start();
t.join();

now suppose above code is in main thread once you say t.join() now thread t will run to completion and only after it completes main thread will continue its work.
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,
This is my conclusion ,please correct me and please mention at which conclusion number i went wrong.

Conclusion:
1)when i create 5 threads and they access a method run (which is not synchronnized),multiple threads can access the same run()

2)when i create 5 threads of the same runnable object ,and they access a method run(which is synchronized),a single thread can access the run()

3)when i create 5 threads of different objects,and they acess the methid run(which is synchronized),multiple thread can access there own run().

4)now when multiple thread access there own run()[which has a static var threaadcounter],it is possible that while one thread(Thread1) executes the increment(say changes the threadcounter from 1 to 2) and is about to print,but before printing another thread (Thread2)increments the static variable(Changes the threadcounter from 2 to 3),resulting into chaging the value of static variable which is reflected over all threads,and when Thread1 prints it it prints the threadcounter as 3(so it missed value 2 in between,it will be printed like 1 then 3)

Then does it mean that synchronized does not matter for different threads having there seperate locks of the object.

Thanks,
With Regds,
Anand
[ September 21, 2005: Message edited by: anand phulwani ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic