Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-Threading not working correctly
    primarykey
    data
    text
    <p><strong>Edit</strong> This post pertains to a homework assignment I have for school that dictates I rely on swing to display my threads and boolean flags for blocking.</p> <p>My application creates a bunch of "job" objects that each contain a thread. Each job belongs to a creature. A creature can possess multiple jobs but can only perform 1 of them at any given moment.</p> <p>My thread utilizes 2 boolean flags to tell whether or not it should be running called "killFlag" and "goFlag". It designates the creature it belongs to as "target". And each target has a boolean "isWorking" to denote whether or not it is busy with another job.</p> <p>This is the thread each job should run:</p> <pre><code>public void run() { long time = System.currentTimeMillis(); long startTime = time; long stopTime = time + 1000 * (long)( jobTime ); double duration = stopTime - time; synchronized (this.target) { while (this.target.isWorking) { status = 'w'; showStatus(); // hmmmmmmmm try { this.target.wait(); } catch (InterruptedException e) { } } this.target.isWorking = true; } while (time &lt; stopTime &amp;&amp; !killFlag) { try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { } if (goFlag) { status = 'p'; showStatus(); time += 100; this.showProgress.setValue((int)(((time - startTime) / duration) * 100)); } else { status = 'r'; showStatus(); } }//End While loop here showProgress.setValue(100); status = 'c'; showStatus(); synchronized (target) { target.isWorking = false; target.notifyAll(); } } </code></pre> <p>At first I thought it was <code>target.notifyAll()</code> because it is throwing an <code>IllegalMonitorStateException</code>, but when I comment it out the thread will objects will construct but when I view them in the GUI 80% of them are displayed as complete without any interation from me and the other 20% are stating that the creature is busy.</p> <p>At first I thought this was because I pop the kill flag too early but when I moved it lower or removed it the symptoms still persist. I'm deployed at the moment and there are no programmers here haha, any advice you could provide would mean the world.</p> <p>For the sake of making sure I provide enough information below are the methods I use for interacting with the threads. The below methods work with a button that changes based on whether or not the thread is running.</p> <pre><code>public void showStatus() { //switch that changes status of button used to start / pause / display status of thread switch (this.status) { case 'r' : startJob.setEnabled(true); startJob.setText ("Run"); break; case 'p' : startJob.setEnabled(true); startJob.setText("Pause"); break; case 'w' : startJob.setEnabled(false); startJob.setText("Working"); break; case 'c' : startJob.setEnabled(false); startJob.setText("Job Complete"); break; } } private class theHandler implements ActionListener {//Listener for Button mentioned above public void actionPerformed (ActionEvent event) { if (event.getSource() == startJob) { if (goFlag) { goFlag = false; } else { goFlag = true; killFlag = false; } } else if (event.getSource() == stopJob) { if (killFlag) { //do nothing } else { killFlag = true; status = 'r'; } } } } </code></pre> <p>This is killing me, I've been digging around to solve this for 6 hours. </p> <p><strong>Edit</strong></p> <p>After adjusting my code based on MadProgrammer's comment the "target.notifyAll()" is fixed. Now the issue seems to be that all threads appear on the display as complete even though the buttons flash randomly between states for a fraction of a second.</p> <p><strong>Edit</strong></p> <p><strong>A lot of edits included below in response to comments</strong></p> <p>Below is how I define the job class where killFlag, goFlag, etc. are defined.</p> <pre><code> class Job extends Item implements SearchableByName, Runnable { int index; String name; int creature; double jobTime; Creature target; boolean goFlag = false; boolean killFlag = false; char status; JButton startJob; JButton stopJob; JProgressBar showProgress; JPanel p1; </code></pre> <p>Below is where Creature (target) is defined where boolean isWorking resides:</p> <pre><code> class Creature extends Entity implements SearchableByName, SearchableByType, Runnable { int party; int empathy; int fear; int carryCapacity; Float age; Float height; Float weight; boolean isWorking = false; </code></pre> <p>And in response to the comments here is a picture of how I am displaying the threads: <img src="https://i.stack.imgur.com/MNWcd.png" alt="enter image description here"> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload