title: Java multithreading task
body: <p><strong>I've got task like this</strong></p>
  
  <p>Build class StringTask, simulating long-term calculations, here consisting of the concatenation of strings.</p>
  
  <p>The class constructor receives as argument the string to duplicate, and the number indicating how many times the string is to be amplified.</p>
  
  <p>Class must implement the Runnable interface, and in its run() method is performed duplicating of a string, it should be done using + used to string variables (it is long-term operation). Usage of the + is a mandatory condition.</p>
  
  <p>The object of class StringTask is treated as a task that can be performed in parallel with each other.</p>
  
  <p>The status tasks are:</p>
  
  <ul>
  <li>CREATED - the task created, but not started yet performed,</li>
  <li>RUNNING - the task is performed in a separate thread</li>
  <li>ABORTED - the task has been interrupted</li>
  <li>READY - the task is successful and are ready results.</li>
  </ul>
  
  <p>Define the following methods in class StringTask:</p>
  
  <ul>
  <li><code>public String getResult ()</code> - returns the result of the
  concatenation</li>
  <li><code>public TaskState getState ()</code> - returns the status of the task</li>
  <li><p><code>public void start ()</code> - running the task in a separate thread</p>
  
  <p><code>public void abort ()</code> - suspends the execution of a task code and operation of the thread</p></li>
  <li><p><code>public boolean isDone ()</code> - returns true if the task is completed
  normally or by interruption, false otherwise</p></li>
  </ul>
  
  <p>The following program code:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) throws InterruptedException {
          StringTask task = new StringTask("A", 70000);
          System.out.println("Task " + task.getState());
          task.start();
          if (args.length &gt; 0 &amp;&amp; args[0].equals("abort")) { 
          /*
              &lt;- here add the code interrupting the task after a second 
              and run it in a separate thread
          */
          }
          while (!task.isDone()) {
              Thread.sleep(500);
              switch(task.getState()) {
                  case RUNNING: System.out.print("R."); break;
                  case ABORTED: System.out.println(" ... aborted."); break;
                  case READY: System.out.println(" ... ready."); break;
                  default: System.out.println("uknown state");
              }
          }
          System.out.println("Task " + task.getState());
          System.out.println(task.getResult().length());
      }
  }
  </code></pre>
  
  <p>run without an argument should put something like this:</p>
  
  <p>Task CREATED
  R.R.R.R.R.R.R.R.R. ... ready.
  Task READY
  70000
  and run with argument abort put something like this:</p>
  
  <p>Task CREATED
  R. ... aborted.
  Task ABORTED
  31700
  Note 1. Main.java file can be modified only in the place marked /* &lt;- */
  Note 2. Do not use the method System.exit(...)</p>
  
  <p><strong>And got this solution</strong></p>
  
  <pre><code>package zad2;
  
  /**
   * Created by szhyhalo on 09.04.17.
   */
  public class StringTask implements Runnable {
  
  
      public enum TaskState {
          CREATED, RUNNING, ABORTED, READY
      }
  
      public boolean isDone = false;
      public TaskState myState;
      public String text;
      public String resultText;
      public Thread myThread;
      public int operations;
  
      public StringTask() {
          myState = TaskState.CREATED;
      }
  
      public StringTask(String a, int i) {
          this();
          text = a;
          operations = i;
          resultText = "";
      }
  
      public boolean isDone() {
          if (myState == TaskState.READY || myState == TaskState.ABORTED) return true;
          return false;
      }
  
      public void abort() {
          while (operations != 0) {
              resultText += text;
              operations--;
          }
      }
  
      @Override
      public void run() {
          while (operations != 0) {
              resultText += text;
              operations--;
          }
          myState = TaskState.READY;
      }
  
      public TaskState getState() {
          return myState;
  
      }
  
      public String getResult() {
          return resultText;
      }
  
      public void start() {
          myThread = new Thread(this);
          myState = TaskState.RUNNING;
          myThread.start();
  
  
      }
  }
  </code></pre>
  
  <p>But after testing, the system give me these <strong>errors</strong>, and i dont know hove to solve them.</p>
  
  <pre><code>Behaviour tests results - scoring decrease = -7
  </code></pre>
  
  <p>WHEN: We are invoking StringTask constructor with arguments 'Y' i 80000, and then we are staring the task</p>
  
  <ul>
  <li>the abort method should have stop execution of the task
  ( abort() method does not terminate the thread )
  
  <blockquote>
    <p>scoring decrease = -7</p>
  </blockquote></li>
  </ul>
  
  <hr>
  
  <p>Requirements tests results - scoring decrease = -2</p>
  
  <p>Case 1: scoring decrease = -1
    - Some variables should have have volatile modifier</p>
  
  <p>Case 2: scoring decrease = -1
    - You should have check interrupted flag by interrupted lub isInterrupted method</p>
  
  <p>How to solve this? I tried, but I have no idea. </p>