CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27

Thread: Multithreading

  1. #16
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  2. #17
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    let the threads then run simultaneously without a race condition. I guess this can be achieved by using semaphores

  3. #18
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multithreading

    Quote Originally Posted by KChad View Post
    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.

  4. #19
    Join Date
    Apr 2014
    Posts
    16

    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

  5. #20
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  6. #21
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multithreading

    Quote Originally Posted by KChad View Post
    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"

  7. #22
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multithreading

    Quote Originally Posted by Norm View Post
    @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.

  9. #24
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  10. #25
    Join Date
    May 2016
    Location
    Navi Mumbai
    Posts
    10

    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.

  11. #26
    Join Date
    Jul 2016
    Posts
    6

    Re: Multithreading

    java beginners lab assignments/ is larger code examples

  12. #27
    Join Date
    Sep 2016
    Posts
    1

    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.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured