This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question on Threads

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I tried the following code and strangely i am getting an unexpected output.

When i try to print the total on lines 3 and 4 within the run() method, it is printed correctly each time.

But when i make a call to the getTotal() method as on lines 1 and 2 , the total is printed different each time! Can anyone please tell me what is going wrong here?

Thanks!

 
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the time line1 & line2 gets executed (in main thread),two threads(th1 & th2) aren't started. So you will get 0 printed for those two lines.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- First of all, you don't need total1 and total2 in MyThread1 class. Just one total will work
- And you may not require name either. As I can see, you are using to make conditions between total1 and total2

The output may be partial for the one but not the other
Or
The output may not be partial for the one and but for the other
Or
The output may not be partial for both
Or
The output may be partial for both

It depends if the run() method would manage to complete itself and able to assign the values to total1, andr total2, before the main thread invoke getTotal(), then none of them would be partial.

where partial = not computed complete list
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line1 and line2..
prints the current value of the totals, in the middle of the execution


Line3 and Line4
prints the total after execution of full list
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adeel Ansari wrote:- First of all, you don't need total1 and total2 in MyThread1 class. Just one total will work
- And you may not require name either. As I can see, you are using to make conditions between total1 and total2

The output may be partial for the one but not the other
Or
The output may not be partial for the one and but for the other
Or
The output may not be partial for both
Or
The output may be partial for both

It depends if the run() method would manage to complete itself and able to assign the values to total1, andr total2, before the main thread invoke getTotal(), then none of them would be partial.

where partial = not computed complete list




Hi Adeel,

Thanks for your reply..

I thought that using the same variable total for both threads is causing the problem. Introduction of Variables total1 and total2 were simply a part of the experimentation to check if that was really the issue!

Anyway, i understood your ans. But if getTotal() appears AFTER calling the run() method in the code sequence, why is it that the execution of the run method is not complete by then?

Thanks.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the fun in threading pal, and therefore we say it sometimes advanced topic. However, all those thread would be independent in execution, so there may be the case where one thread ends up its execution while the other still in execution. I hope you get my point.

Well, I think I should elaborate it more. Actually, here its not about what appears first in sequence. All the threads are given with their work, and they got started on it. Now, its on scheduler that which one got more time to complete its work. Every thread would be given an opportunity to complete its task, but how is depends on the underlying scheduler, i.e. operating system. It may be the case where main thread which spawns other two threads completes its execution before any of those, then the program would wait for both of those thread to complete their tasks and then exits.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, precisely and specifically, main thread would not hold there and wait other two threads to complete their job, until and unless its made to wait by calling wait() on it. After spawning both, it would straight go to the next line and execute that, if it gets the turn. It wouldn't wait for them to complete their tasks.

Now I am pretty much hopeful that I clarify the thing.
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adeel Ansari wrote:Okay, precisely and specifically, main thread would not hold there and wait other two threads to complete their job, until and unless its made to wait by calling wait() on it. After spawning both, it would straight go to the next line and execute that, if it gets the turn. It wouldn't wait for them to complete their tasks.

Now I am pretty much hopeful that I clarify the thing.



Yeah! Pretty much!
Thanks.. Will catch hold of you the next time i have a problem with threads!
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Just hang here for sometime and you will learn a lot, from folks a lot more knowledgeable and senior than me.

You are welcome, Pats.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic