Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Thread Pool and Scheduler

  1. #1
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Thread Pool and Scheduler

    Questions regarding Threading. The structure of my code is...

    The UI thread calls a Routine R0 which *uses a background thread* to call two Routines R1 and R2 which each need to retrieve information from the internet. R1 and R2 are called in succession. R1 must complete before R2 is called. This sequence is done in a loop, with each iteration selecting the next item from an ArrayList to operate on.

    *Note* I believe that R0 does not need to use a background thread as it is R1 and R2 which go to the internet. But it works in simple testing so I'll leave it as is for now.

    R1 and R2 each extend Thread, so each begins with a public void run() {...}. Also each needs to store a result on the UI thread so each uses a runOnUiThread(new Runnable(...)) at the end of its processing.

    All of the code works perfectly (with a bit of cheating, a finite loop after calls to R1 and R2 waiting for the result) when tested in a simple environment where there is no possibility of a conflict. When installed in the App it works perfectly for the first iteration then the App "hangs" - it does not finish the next
    iteration, it literally just sits there; no processing is going on as the LogCat has paused; and the program does respond to other menu selections correctly - so it has not "crashed";

    I have not been this deep into Threads so I have been reading and getting confused by several blogs posts etc on the topic of Threads, so I have some questions...

    Do I need to use a thread pool?
    Will I need to use a Scheduler?
    Does anyone have a favorite, good basic introduction to this topic? Especially one with an introductory level example?

    Hopefully, next time I will have some code to show

    Thank you,
    Mick

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Thread Pool and Scheduler

    then the App "hangs"
    Sounds like the threads may be hung waiting on some event that never happens.
    I use print statements for debugging. Try adding lots of print statements so you can see where the code is executing and not executing(ie is hung). It can be an iterative process to get the print statements in the right place.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Thread Pool and Scheduler

    Hi Norm,
    Ooops, I neglected to mention that this is an Android App

    Each of tasks R1 and R2 accesses the internet, which is a relatively long running process. But each starts that access then returns immediately. I must delay the start of R2 until after that "long running" R1 process finishes. I believe that is why R1 and R2 need to be in different threads, and need to be run by a scheduler. Unless there is another option.

    As I mentioned the code works perfectly when the components are run independently of each other. But in the App, R1 and R2 are in a loop processing data from an ArrayList. Each uses a single-statement runOnUiThread to store the result. If I put a finite delay loop after each, waiting for the new value to appear in a global String result field, it all works. But such a delay loop is problematic in Android. I need to find a better technique than this...
    result = " ";
    while (result.equals(" "));
    I have more than 50 years experience in technical computer programming so I am not new to concurrent processing, but I am new to this in Android; and it is a daunting topic. This is why I am looking for a good starting point, especially something with an example.
    Mick

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Thread Pool and Scheduler

    Also posted here: https://coderanch.com/t/770327/mobil...Pool-Scheduler
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: September 2nd, 2014, 05:35 AM
  2. Thread pool in a web server in Java
    By shiroyamacah in forum Threads
    Replies: 0
    Last Post: May 30th, 2014, 03:01 PM
  3. Help with my CPU Scheduler
    By HacKofDeatH in forum Object Oriented Programming
    Replies: 6
    Last Post: October 12th, 2011, 12:50 AM
  4. Help w/ my CPU Scheduler!!
    By HacKofDeatH in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2011, 12:58 AM
  5. thread pool question
    By balexios in forum Threads
    Replies: 3
    Last Post: October 24th, 2010, 03:47 AM

Tags for this Thread