Faalhaaz
Faalhaaz

Reputation: 41

Sleep a Thread in a loop

I have to make a DieTester for school. One that rolls the dice 100 times and then puts the output in a Table Chart and another table. The problem is that my Thread wont sleep with the time that is set by the Slider. Here my DieTester:

package sample.Controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.RunnableFuture;

public class DieTester implements Runnable{
private Thread t;
private String Threadname;
List<Integer> List = new ArrayList();
Random rand = new Random();

long l;

public DieTester(String name){
    Threadname = name;
}

public void run() {

    for (int n = 0; n < 100; n++) {
        try {
            Thread.sleep(getTime());
            List.add(rand.nextInt(6) + 1);
            System.out.println(List.get(n));

        } catch (InterruptedException e) {
            System.out.println("Error");
        }
    }
}

public void start(){
    if (t == null)
    {
        t = new Thread (this, Threadname);
        t.start ();

    }
}

public void setTime(double SliderTime){
    l = (long) SliderTime;
}

public long getTime(){
    return l;
}
}

Here the controller:

package sample.Controllers;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Slider;


public class Controller {

DieTester dice = new DieTester("Time");

double time=0;
EventHandler e = new EventHandler() {
    @Override
    public void handle(Event event) {
        time = TimeSlider.getValue();
    }
};


@FXML
Slider TimeSlider = new Slider(50, 2000, 50);




@FXML
public void HandlePauseResumeAction(){
}

@FXML
public void HandleStartAction(){
    DieTester die = new DieTester("Start");
    die.start();

}

@FXML
public void HandleSlider(){

    TimeSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
        time = TimeSlider.getValue() * 20;
        //System.out.println(time);

        dice.setTime(time);

    });

    System.out.println(dice.getTime());



}



}

The slider and everything is set up properly. And if I call the getTime() it puts out the time properly, but the Thread isn't sleeping or something.

Upvotes: 1

Views: 269

Answers (2)

Faalhaaz
Faalhaaz

Reputation: 41

Ok guys so I figured it out, this is what I did

 @FXML
public void HandleStartAction(){
    start();
}

public void run(){
    for(int n = 0; n < 100; n++){
        try {
            if(!suspend){
                Thread.sleep((long)TimeSlider.getValue() * 20);
                List.add(rand.nextInt(6) + 1);
                System.out.println(List.get(n));
            }else{

            }

        } catch (InterruptedException e) {
            System.out.println("Error");
        }
    }
}

public void start(){
    if (t == null)
    {
        t = new Thread (this, "Start");
        t.start ();

    }
}

Upvotes: 0

bowmore
bowmore

Reputation: 11280

This is a mutable shared variable :

long l

Threads access it concurrently, (one reads, one writes), yet it doesn't have proper synchronization, so writes of one thread are not guaranteed to be seen by the other thread.

On top of that, l is initialized to 0, and odds are the spawned thread has raced through 100 loops, without really sleeping, before the first property change event happens.

Upvotes: 1

Related Questions