-
May 17th, 2016, 06:26 PM
#16
Re: Multithreading
After looking more at the Semaphore API doc, I'm not sure how it could be used.
Can you define what the program needs to do with the threads? Is there an order to execute them?
What is the relationship between the checking, saving and transfer threads?
Last edited by Norm; May 17th, 2016 at 06:31 PM.
Norm
-
May 19th, 2016, 08:48 PM
#17
Re: Multithreading
let the threads then run simultaneously without a race condition. I guess this can be achieved by using semaphores
-
May 20th, 2016, 12:37 AM
#18
Re: Multithreading
Originally Posted by KChad
let the threads then run simultaneously without a race condition. I guess this can be achieved by using semaphores
If two theads are accessing the same data, and one or more of the threads are writing to the data, the data will need to be synchronized, otherwise you'll get a race condition.
I written a few thread synchronization articles, you might want to check them out to get some background.
-
May 20th, 2016, 05:33 PM
#19
Re: Multithreading
yeah synchronizing is also possible, but I am really not sure how to go about it. My main task was to solve the banking problem, but later I found out there was a race condition
can someone help me please
-
May 20th, 2016, 05:58 PM
#20
Re: Multithreading
solve the banking problem
Can you explain again what the banking problem is?
Is the race condition you talk of caused by the transfer being done BEFORE the other transactions are executed?
Should the transfer be done AFTER the updates to the savings and checking accounts?
Or can it be done before either of them?
can someone help me please
Here is a simple solution:
Code:
checkInterest.start();
saveInterest.start();
// Thread.sleep(50); //<<<<<<<<<<< transfer NEEDS to wait until above are finished !! This WORKS
saveInterest.join(); //<<<<<<<<< or wait until money there
transfer.start();
Last edited by Norm; May 20th, 2016 at 06:00 PM.
Norm
-
May 20th, 2016, 06:49 PM
#21
Re: Multithreading
Originally Posted by KChad
yeah synchronizing is also possible, but I am really not sure how to go about it.
Probably the best thing you can do is learn the underlying concepts (and then apply them to your specific problem).
Search bing or google for "thread synchronization in java tutorial"
-
May 20th, 2016, 07:08 PM
#22
Re: Multithreading
@Arjay - How can thread synchronization be used to solve the problem where one thread is required to run after the other threads have completed?
Norm
-
May 21st, 2016, 12:21 AM
#23
Re: Multithreading
Originally Posted by Norm
@Arjay - How can thread synchronization be used to solve the problem where one thread is required to run after the other threads have completed?
It's not real clear what the problem is. ...but, having a good grasp of threading fundamentals is never going to hurt.
-
May 21st, 2016, 06:40 AM
#24
Re: Multithreading
It's not real clear what the problem is
I think the problem would be clear if an attempt is made to debug it. For example adding some print statements to show when the accounts' balances change.
Norm
-
July 5th, 2016, 03:47 AM
#25
Re: Multithreading
Threads share a common memory area in case of Java multithreading. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
-
July 31st, 2016, 06:56 AM
#26
-
September 5th, 2016, 05:21 AM
#27
Re: Multithreading
Glad to read this, The program creates and starts 3 threads. square measure there any necessities that|that} thread runs initial and which thread ought to run last? will the threads be run in any order?
As coded the threads will execute in any order, eg the transfer might be run initial.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|