Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:56:33+00:00 2026-06-04T06:56:33+00:00

I’m having a bit of a problem with writing a multithreaded algorithm in Java.

  • 0

I’m having a bit of a problem with writing a multithreaded algorithm in Java. Here’s what I’ve got:

public class NNDFS implements NDFS {

//Array of all worker threads
private Thread[] threadArray; 

//Concurrent HashMap containing a mapping of graph-states and 
//algorithm specific state objects (NDFSState)
private ConcurrentHashMap<State, NDFSState> stateStore;

//Whether the algorithm is done and whether a cycle is found
private volatile boolean done;
private volatile boolean cycleFound;


/**
  Constructor that creates the threads, each with their own graph

  @param  file        The file from which we can create the graph
  @param  stateStore  Mapping between graph-states and state belonging to our algorithm
  @param  nrWorkers   Number of working threads we need
 */
public NNDFS(File file, Map<State, NDFSState> stateStore, int nrWorkers) throws FileNotFoundException {
    int i;
    this.stateStore = new ConcurrentHashMap<State, NDFSState>(stateStore);
    threadArray = new Thread[nrWorkers]; 
    for(i=0;i<nrWorkers;i++){
         Graph graph = GraphFactory.createGraph(file);
         threadArray[i] = new Thread(new NDFSRunnable(graph, i));
    }
}

/**
    Class which implements a single thread running the NDFS algorithm
 */
class NDFSRunnable implements Runnable{

    private Graph graph;

    //Neccesary as Java apparently doesn't allow us to get this ID
    private long threadId; 

    NDFSRunnable(Graph graph, long threadId){
         this.graph = graph;
         this.threadId = threadId;
    }

    public void run(){
        try {
            System.out.printf("Thread id = %d\n", threadId);
            //Start by executing the blue DFS for the first graph
            mcdfsBlue(graph.getInitialState(), threadId);
        } catch (CycleFound e) {
            //We must catch all exceptions that are thrown from within our thread
            //If exceptions "exit" the thread, the thread will silently fail
            //and we dont want that. We use 2 booleans instead, to indicate the status of the algorithm

            cycleFound = true;
        }

        //Either the algorithm was aborted because of a CycleFound exception
        //or we completed our Blue DFS without finding a cycle. We are done!
        done = true;
    }

    public void mcdfsBlue(State s, long id) throws CycleFound {
        if(done == true){
           return;
        }
        //System.out.printf("Thread %d begint nu aan een dfsblue\n", id);
        int i;
        int counter = 0;
        NDFSState state = stateStore.get(s);
        if(state == null){
                state = new NDFSState();
                stateStore.put(s,state);
        }
        state.setColor(id, Color.CYAN);
        List<State> children = graph.post(s);
        i = state.incNextBlue();
        while(counter != children.size()){
            NDFSState child = stateStore.get(children.get(i%children.size()));

            if(child == null){
                    child = new NDFSState();
                    stateStore.put(children.get(i % children.size()),child);
            }
            if(child.getLocalColor(id) == Color.WHITE && !child.isRed()){

                mcdfsBlue(children.get(i % children.size()), id);
            }
            i++;
            counter++;
        }
        if(s.isAccepting()){
            state.incRedDFSCount();
            mcdfsRed(s, id);
        }
        state.setColor(id, Color.BLUE);
    }

    public void mcdfsRed(State s, long id) throws CycleFound {
        if(done == true){
            return;
        }
        int i;
        int counter = 0;
        NDFSState state = stateStore.get(s);
        state.setPink(id, true);
        List<State> children = graph.post(s);
        i = state.incNextRed();
        while(counter != children.size()){
            NDFSState child = stateStore.get(children.get(i%children.size()));
            if(child == null){
                    child = new NDFSState();
                    stateStore.put(children.get(i%children.size()),child);
            }
            if(child.getLocalColor(id) == Color.CYAN){
                throw new CycleFound();
            }
            if(!child.isPink(id) && !child.isRed()){
                mcdfsRed(children.get(i%children.size()), id);
            }
            i++;
            counter++;
        }
        if(s.isAccepting()){
            state.decRedDFSCountAndWait();
        }
        state.setRed();
        state.setPink(id, false);
    }

}

public void init() {}

public void ndfs() throws Result {
    int i;
    done       = false;
    cycleFound = false;
    for(i=0;i<threadArray.length;i++){
            System.out.printf("Launch thread %d\n",i);
            threadArray[i].run();
    }

    try {
        for(i=0;i<threadArray.length;i++){
            threadArray[i].join();
        }
    } catch (InterruptedException e) {

    }

    //We want to show the result by throwing an exception (weird, but yeah :-/)
    if (cycleFound) {
      throw new CycleFound();
    } else {
      throw new NoCycleFound();
    }
}                               

}

However, when I run this, it seems like the first thread is called, completes, and then the next is called etc. What I want obviously, is that all threads are started simultaneously! Otherwise the algorithm has very little use…

Thanks for your time/help!

Regards,

Linus

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:56:34+00:00Added an answer on June 4, 2026 at 6:56 am

    Use threadArray[i].start(); to launch your thread.

    If you use threadArray[i].run();, all it does is call the method normally, in the same thread as the caller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.