Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule threads in java whose tasks depend on one another?

This is my first attempt at multithreading after learning about threads (in theory, heh) in my OS class at school, and I think the way I've gone about doing what I'm trying to do is bad practice/sloppy.

I'm parallelizing a minimax algorithm by spawning a separate thread for each branch of the game on which the algorithm is set to run. The scheduling part of this is a little tricky; as the algorithm deepens iteratively, and I want depth consistency across all the threads.

So, first I have a master thread which spawns a subthread for each available move in the game:

    public void run(){
        // initializes all the threads
        for (AlphaBetaMultiThread t : threadList) {
            t.start();
        }
        try { //This won't ever happen; the subthreads run forever
            evals = 0;
            for (AlphaBetaMultiThread t : threadList) {
                t.join();
                evals += t.evals;
            }
        } catch (Exception e) {
            System.out.println("Error joining threads: " + e);
        }
    }

The thread passes itsself to the constructor so each subthread so that the threads can access the master thread's maxDepth property and signalDepth methods:

    public synchronized void signalDepth(){
        signals++;
        if (signals % threadList.length() == 0){
            if (verbose)
                System.out.println(toString());
            depth++;
        }
    }

And finally, here's the subthread evaluation process. Whenever it's ahead of the rest of the threads, it lowers its own priority, and then yields until all the subthreads have signalled.

public void run() {
    startTime = System.currentTimeMillis();
    while(true){
        if (depth >= master.maxDepth) {
            this.setPriority(4);
            this.yield();
            break;
        } else {
            this.setPriority(5);
        }
        eval = -1*alphabeta(0, infHolder.MIN, infHolder.MAX);
        manager.signalDepth();
        depth += 1;
    }
}

Besides the fact that my implementation seems not to work at all right now (still trying to figure out why), I really feel as if what I'm doing isn't the standard way of doing things. My intuition is that there are probably all kinds of built-in multithreading libraries that could make my life a lot easier, but I don't really know what I'm looking for.

Oh, I'm also getting a warning that Thread.destroy() is deprecated (which is how I'm planning on destroying everything after the computer player finally plays its move).

I guess my question is this: what should I be using to manage my subthreads?

edit: Oh, and if there's stuff I've left out that is relevant to my question, feel free to look at my complete code on github: https://github.com/cowpig/MagneticCave The relevant files are GameThread and AlphaBetaMultiThread. I apologize for my cluelessness!

Another edit: I want the threads to deepen iteratively forever, until the gamePlayer object (the one creating the master thread) decides it is time to choose a move-- at which it will access the list of moves and find the one with the highest evaluation. This means .join() won't work, unless I create a new set of threads for every depth iteration, but then that would require a lot more overhead (I think) so I don't really want to have to do that.

like image 326
mavix Avatar asked Mar 06 '26 11:03

mavix


2 Answers

Your intuition is correct. Java 5 introduced a slew of useful concurrency constructs. One in particular you might want to research is CyclicBarrier. It is a synchronization aid that allows multiple threads to wait on each other to reach a common, barrier point (in your case, that would be the master depth).

like image 114
Perception Avatar answered Mar 08 '26 23:03

Perception


You should be using Thread.join() to wait for the sub-threads, which should just exit when done. No need for Thread.destroy() at all, and no need for fiddling with priorities either.

like image 37
user207421 Avatar answered Mar 09 '26 00:03

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!