• 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 synchronized is not working as expected

 
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating multiple threads and want to print the Thread Names in an ascending order not randomly generated .

Below is my code,




The output I am getting is


Thread2 of current thread
Thread4 of current thread
Thread6 of current thread
Thread8 of current thread
Thread10 of current thread
Thread1 of current thread
Thread3 of current thread
Thread5 of current thread
Thread7 of current thread
Thread9 of current thread


what is happening wrong here?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't done anything to constrain the order.

For a start, the synchronization in that example does nothing. Each ThreadExample object synchronizes on itself. So they are all synchronized on different objects - which means it isn't restricting them at all.

But even if they were all synchronized on the same object all that would do is stop them running their run() methods at the same time. It would still put no constraints at all on the order they run them.

To constrain the order (and assuming you don't want to just use a single thread, which is the obvious way to do it!) you'll need to come up with a way for a Thread to wait for another Thread to complete before continuing. There is a method in the Thread class that would help you out there.
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used obj.join() method surrounded with try...catch block and it is working fine.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sharmistha Sarkar wrote:I have used obj.join() method surrounded with try...catch block and it is working fine.



... except that it isn't really multi-threaded is it? Yes, there are more than one thread, but you are making sure that a thread is started, and finished, before starting the next one. There is only one thread, in addition to the main thread, running at any given time. Can you not accomplish the same thing, by not using threads? Instead, just have the main thread call the run method directly?

Henry
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is only one thread, in addition to the main thread, running at any given time. Can you not accomplish the same thing, by not using threads? Instead, just have the main thread call the run method directly?




Hi Henry,

There is a thread [2nd thread] with the main thread , If I call the 2nd thread with a run(), then the 2nd thread actually not getting created , it remains the same main thread only. The method start() actually calls the run() internally and creates the new thread.

Look at my code below,



Output:

What is the thread name? :main
hello
What is the thread name? :Thread-0

but if I use
object.run();

Output

What is the thread name? :main
hello
What is the thread name? :main

It is not creating the new thread "Thread-0"

Is this correct answer?
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am facing issue in thread Synchronization. I have created a variable called accountBalance; where I am storing 20,000.00 . I am creating 10 threads to debit 2,000.00 from the same variable. I want after 10th thread debit 2,000.00 from accountBalance, My final accountBalance should be 0000.00. But every time for every thread I am getting output 18,000.00. It should come like Thread 1 18000.00, Thread 2 16000.00 like this...


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because accountBalance is an instance variable, and each Feb5 object has its own copy. Make the field static and see what happens. (You should also make it volatile in order for its value to be visible correctly across threads.)

By the way, don't ever give classes or packages the name of an existing class or package in the JRE ("Thread"). And packages names are all lowercase by convention.
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ulf,

I have made the variable static. It is working fine. I don't find reason to make it volatile, because it is visible to all threads.

Here is my modified code,

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't find reason to make it volatile, because it is visible to all threads.


In this code - yes, because the threads do so little work that they probably do not get switched out at runtime at all. That obviates the need for volatile. In all likelihood, it even obviates the need for any synchronization. But for long-running threads (that will get switched out) where the correct value of the variable needs to be visible to all threads you most certainly need volatile.

As an aside, you should never use float or double for currency amounts - always use exact data types like int or BigDecimal.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have given a try to this program, please check.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an aside, since it hasn't been pointed out before: extending Thread is almost always the wrong way to do things - implementing Runnable, or Callable, or Future, is a better approach. See https://coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable for details.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic