pausing threads in java - java

I was wondering if someone coule help me on how to implement a thread which runs infinitely in the background until the user presses p at which point it pauses the other threads and upon pressing r resumes other threads. This is some of the code
Some of the main object
public class CardGame
{
static Player[] players;
static int handSize;
static Queue<Card>[] playingDeckArray;
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println( "\nHello, how many players would you like" );
int playersNum = Integer.parseInt(Checks.userInputCheck( "\\d" ));
System.out.println( "\nHow many cards should each player begin with" );
int handSize = Integer.parseInt(Checks.userInputCheck( "\\d" ));
System.out.println( "\nWhich strategy would you like to use 1 or 2" );
int strategy = Integer.parseInt(Checks.userInputCheck( "[12]$" ));
Logger.createDeck( playersNum, handSize );
makePlayers( playersNum, handSize, strategy );
makePlayingDecks( playersNum );
dealInitialHand( playersNum, players, handSize );
makePlayerOutputs();
for ( int i = 0; i < players.length; i++){
logInitialHand(players[i]);
}
CardGame cG = new CardGame();
cG.startPauseThread();
for ( int i = 0; i < players.length; i++){
new Thread(players[i]).start();
}
}
public void startPauseThread(){
Thread add = new Thread( pauseInputThread );
add.start();
}
Thread pauseInputThread = new Thread(){
public void run(){
int i = 0;
for(;;){
System.out.println("i'm still here" );
Scanner reader = new Scanner(System.in);
String result = Checks.userInputCheck( "[pPrR]$" );
i++;
System.out.println(i);
}
}
};
}
The player object which are the threads to be paused
public class Player implements Runnable
{
Card[] hand;
String playerName;
int strategyChosen;
public void run(){
System.out.println( "les do dis" );
}
private Player(){
}
public Player( int strategy, int cardsInHand, int playerNumber ){
hand = new Card[cardsInHand];
strategyChosen = strategy;
playerName = "Player " + playerNumber;
}
public String getPlayerName(){
return playerName;
}
public void fillHand(){
for ( int i = 0; i < hand.length; i++){
hand[i] = new Card(0);
}
}
public void setHand( int value, int index ){
hand[index].setCardValue( value );
}
public void seeHand(){
for ( int i = 0; i < hand.length; i++){
System.out.println( hand[i].getCardValue() );
}
}
public String getHand(){
String result = "";
for ( int i = 0; i < hand.length; i++ ){
result = result + hand[i].getCardValue() + " \n" ;
}
return result;
}
public int getHandValue( Card card ){
return card.getCardValue();
}
}
The players will be 'playing a game' where they draw and discard objects from arrays, but the user should be able to pause and resume the programm at any point during the game. i just dont quite understand how to go about that, using events and listners.
Help would be appreciated. Thank you.

Thread has no mechanism for this on its own. You can, however, easily add this functionality in to your own process. You'll have some flag to set and check and then probably the best is to have a wait/notify scheme. Here's a simple demonstration:
abstract class PausableTask implements Runnable {
private volatile boolean paused;
private final Object lock = new Object();
void setPaused(boolean shouldPause) {
synchronized (lock) {
paused = shouldPause;
if (!paused) {
lock.notify();
}
}
}
boolean isPaused() { return paused; }
#Override
public void run() {
for (;;) {
synchronized (lock) {
while (paused) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
}
doTask();
}
}
abstract void doTask();
}
class Counter {
volatile long count;
public static void main(String[] args) {
final Counter counter = new Counter();
PausableTask increment = new PausableTask() {
#Override
void doTask() {
counter.count++;
}
};
PausableTask decrement = new PausableTask() {
#Override
void doTask() {
counter.count--;
}
};
decrement.setPaused(true);
PausableTask next = increment;
Scanner in = new Scanner(System.in);
long count = counter.count;
new Thread(increment).start();
new Thread(decrement).start();
for (;;) {
System.out.print(
(next == increment ? "Counting up from " : "Counting down from ")
+ count + ". Enter 'exit' to abort or anything else to toggle: "
);
if (in.nextLine().equals("exit")) {
System.exit(0);
}
if (increment.isPaused()) {
next = increment;
decrement.setPaused(true);
} else {
next = decrement;
increment.setPaused(true);
}
count = counter.count;
next.setPaused(false);
}
}
}
For taking user input from the keyboard, there's not really a convenient way to do that in Java. If you want to take straight keystrokes you need a GUI component for them to happen in. If you do plan on implementing a GUI take a look at the key bindings tutorial.

It's not possible to pause a thread simply by calling some predefined method, nor it's possible to kill a thread. The only thing you can do is to implement this behavior by yourself. It means that you will do something like this:
public class MyThread extends Thread {
private volatile boolean running = true;
private volatile boolean paused = false;
public void kill() {
this.running = false;
}
public void setPaused(boolean paused) {
this.paused = paused;
}
#Override
public void run() {
while (running) {
if (!paused) {
// do another step
}
}
}
}
However, it's possible that I didn't understand your question correctly. Leave me a feedback so I can update my answer eventually.

Related

How to make two Threads wait and notify the correct way in Tic Tac Toe game

I followed some discussion on the website and implemented some suggestions but I was not able to complete the task.
I have this assignment to build Tic Tac Toe game in Java.
There's a lot of tutorials on the web I know, but I wanted to build it using threads for each player so that only one player at a time could enter X/O to the board and I was not able to figure out the implementation for the wait/ notify.
To be honest, I find it a bit confusing! I guess the logic is 90% done, and I am wondering how to finish it.
Basically, I have these classes:
abstract public class Player extends Thread
public class HumanPlayer extends Player
public class MachinePlayer extends Player
public class Board
public class Game
public class Main
I'll paste first the players classes. Notice the run method I tried to Synchronize on top of the gameOver variable:
Player.java
abstract public class Player extends Thread {
abstract int getMyTurn() ;
abstract String getPlayerName() ;
abstract void setPlayerName(String n ) ;
abstract void setSign(int n ) ;
abstract int getSign() ;
}
MachinePlayer.java
import java.util.Random;
public class MachinePlayer extends Player {
private String name ;
private int sign ;
Board board ;
public MachinePlayer(Board board) {
this.board = board;
}
#Override
public void run() {
System.out.println("Player "+name+" turn:" );
while (!board.isGameOver())
{
board.setCurrentPlayerTurn(this.getSign()); // i think it keeps track of which player is holding the lock ?
Move move = generateRandomMove();
board.makeMove(this, move.getX(), move.getY() );
}
}
#Override
int getMyTurn() {
return 0;
}
#Override
public String getPlayerName() {
return name;
}
#Override
public void setPlayerName(String name) {
this.name = name;
}
#Override
void setSign(int sign) {
this.sign = sign ;
}
#Override
int getSign() {
return sign;
}
private Move generateRandomMove(){
while (true)
{
int x = getRandomNumber(0,board.getBoardSize()) ;
int y = getRandomNumber(0,board.getBoardSize()) ;
if (board.isPositionAvalabile(x, y ))
{
return new Move(x,y);
}
}
// todo implement the best move !
}
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
}
HumanPlayer.java
public class HumanPlayer extends Player {
private String name ;
private int sign ;
private Board board ;
public HumanPlayer(Board board) {
this.board = board;
}
#Override
public void run() {
while (!board.isGameOver())
{
board.setCurrentPlayerTurn(this.getSign()); // I think it keeps track of which player is holding the lock ?
Move move = requestMoveFormHuman(); // request move for human player // will be changed in the GUI?
board.makeMove(this, move.getX() , move.getY()); // check for move logic here >
}
}
#Override
int getSign() {
return sign;
}
#Override
int getMyTurn() {
return 0;
}
#Override
public String getPlayerName() {
return name;
}
#Override
public void setPlayerName(String name) {
this.name = name;
}
#Override
void setSign(int n) {
this.sign = n ;
}
// logic
private Move requestMoveFormHuman(){
int i=0 ;
while (true)
{
System.out.println(i);
Move move = requestMove() ;
int x = move.getX();
int y = move.getY();
if (board.isPositionAvalabile(x,y))
{
return new Move(x,y);
}
}
}
private Move requestMove() {
System.out.println("Player "+name+" turn:" );
System.out.println("Enter Row number: ");
int x = ScanInt();
System.out.println("Enter Coulmn number ");
int y = ScanInt();
return new Move(x ,y ) ;
}
private int ScanInt(){
Scanner scan = new Scanner(System.in);
// This method reads the number provided using keyboard
int num = scan.nextInt();
// Closing Scanner after the use
return num;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Game game = new Game() ;
game.start();
}
}
Board.java
import java.util.HashMap;
import java.util.Scanner;
public class Board {
private int currentPlayerTurn = 0 ;
public int getBoardSize() {
return boardSize;
}
private int boardSize ;
private int[][] board_values ;
private boolean gameOver ;
private HashMap<String , String> result_history;
public Board(int boardSize) {
this.boardSize = boardSize;
board_values = new int[boardSize][boardSize];
for (int i = 0; i < boardSize; i++) {
for (int j = 0 ; j < boardSize; j++)
board_values[i][j] = 0 ;
}
System.out.println("");
}
private String getSign(int n) {
if (n==1)
return "X" ;
if (n==2)
return "0" ;
return " " ; // empty cell
}
public void printBoard(){
// todo change to ui components
for (int i = 0 ; i < boardSize ; i++)
{
for (int j = 0 ; j < boardSize ; j++)
{
System.out.print("|");
System.out.print(" " + getSign(board_values[i][j])+ " ");
System.out.print("|");
}
System.out.println("");
System.out.println("----------------------------------" );
}
}
/** is this the Synchronized one ? */
public boolean makeMove(Player player , int x , int y ) {
/*Update Board Value
insert 1 in the 2d board at x,y for player 1
insert 2 in the 2d board at x,y for player 2
*/
int sign = player.getSign();
updateBoard(x,y , sign ) ;
/*print Board*/
printBoard();
/* check for winner */
doWeHaveAwinner(x,y , sign);
return true ;
}
/*check the whole row (x) for if all signs are equals
, checks the whole column (x) for if all signs are equals
check diagonal if x=y */
private boolean doWeHaveAwinner(int x, int y, int sign) {
// TODO: 16/01/2022
return false ;
}
private void updateBoard(int x, int y , int sign ) {
board_values[x][y] = sign ;
}
public boolean isPositionAvalabile(int x , int y ){
return !(x>this.boardSize || x < 0 || y>this.boardSize || y < 0) ;
}
private void checkMove() {
// todo > ?
}
private void resetBoard(){
// todo set al values to 0
}
private void updateResultHistory(){
// TODO: 16/01/2022 update <Winner Name , Ps+1 >
}
private int ScanInt(){
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
// This method reads the number provided using keyboard
int num = scan.nextInt();
// Closing Scanner after the use
return num;
}
/*Get Set*/
public synchronized boolean isGameOver() {
return gameOver;
}
public void setGameOver(boolean gameOver) {
this.gameOver = gameOver;
}
public int getCurrentPlayerTurn() {
return currentPlayerTurn;
}
public void setCurrentPlayerTurn(int currentPlayerTurn) {
this.currentPlayerTurn = currentPlayerTurn;
}
}
And here is Game.java, it basically inits 2 players.
Notice that in each player init I passed the board instance as a parameter.
It might a look little bit messy but I think passing the board instance for the players is the important thing here. Also I called the player1().start and player2.start()
Game.java
public class Game {
private Player player1 ;
private Player player2 ;
private Board board ;
private void initBoard(){
/* request board size */
int board_size = requestBoardSize();
board = new Board(board_size); // Synchronized class fields ?
}
private void initGameMode(){
/*
init the players objects as follows
1 - for human-machine game
2 - for machine-human game, and
3 - for human-human game
*/
System.out.println(Utils.str_gameType);
Scanner scanner = new Scanner( System.in);
int game_mode = scanner.nextInt( );
switch (game_mode) {
case 1:
/* init game type1 human-machine Game*/
player1 = new HumanPlayer(board);
player2 = new MachinePlayer(board);
break;
case 2:
player1 = new MachinePlayer(board);
player2 = new HumanPlayer(board);
break;
case 3:
player1 = new HumanPlayer(board);
player2 = new HumanPlayer(board);
break;
default:
throw new IllegalStateException("Unexpected value: " + game_mode);
}
player1.setSign(1);
player2.setSign(2);
System.out.println("");
}
private void initPlayersName(){
/*request name player 1 */
System.out.println(Utils.str_player1Name); /*Scan first name*/
Scanner scan1 = new Scanner(System.in);
String name1 = scan1.next();
player1.setPlayerName(name1);
/*request name player 2 */
System.out.println(Utils.str_player2Name); /*Scan first name*/
Scanner scan2 = new Scanner(System.in);
String name2 = scan2.next();
player2.setPlayerName(name2);
}
public Game() {
System.out.println(Utils.str_start_game);
initBoard();
initGameMode();
initPlayersName();
}
public void start() {
// todo change who starts first
player1.start();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(300);
player2.start();
}catch (Exception e)
{
}
}
}).start();
}
private int requestBoardSize() {
System.out.println(Utils.str_request_board_size);
Scanner scanner = new Scanner(System.in) ;
int i = scanner.nextInt() ;
return i ;
}
}///End of Class

How can I distinguish a winner in this thread race?

I have been writing a race code for a class I am in that races two threads, a tortoise and a hare. I can get both of them to run for 80 units but I don't know how to write a code that determines and outputs who the winner is. Any help would be appreciated because I am super new to coding.
I have the tortoise, hare, and raceParticipant classes. My driver class looks like this, where I would assume I put the winner code?
package Domain;
public class Driver
{
public static void main(String[] args)
{
Hare bob = new Hare();
Tortoise fred = new Tortoise();
int winDistance = 80;
do {
bob.sprint();
fred.sprint();
bob.display();
fred.display();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}while(bob.getTotalDistance() < winDistance && fred.getTotalDistance() < winDistance);
}
}
My sprint method is
public int sprint()
{
int sleep = generator.nextInt(100);
int sprintDistance = 0;
if (sleep > sleepPercent)
{
sprintDistance = generator.nextInt(topSpeed) + 1;
}
totalDistance +=sprintDistance;
return sprintDistance;
}
I don't see you creating a new thread anywhere.
You can create a Hare class like this:
public class Hare implements Runnable {
private static final int SLEEP_DURATION = 3000; //milliseconds
private static final int SPEED = 3; //units per second
private int distanceToRun;
private final RaceFinishListener listener;
public Hare(int distanceToRun, RaceFinishListener listener) {
this.distanceToRun = distanceToRun;
this.listener = listener;
}
#Override
public void run() {
do {
distanceToRun -= SPEED;
try {
Thread.sleep(SLEEP_DURATION);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (distanceToRun > 0);
listener.onRaceFinished(getClass().getSimpleName());
}
}
and a similar Tortoise class with these variables:
private static final int SLEEP_DURATION = 1000; //sleeps less
private static final int SPEED = 1; //but is slow
Then create a listener to get notified when someone has finished:
public interface RaceFinishListener {
void onRaceFinished(String finisher);
}
and finally your main class:
public class Test implements RaceFinishListener {
private String winner;
private static final int DISTANCE_TO_RUN = 10;
public static void main(String[] args) {
new Test().race();
}
private void race() {
Hare bob = new Hare(DISTANCE_TO_RUN, this);
Tortoise fred = new Tortoise(DISTANCE_TO_RUN, this);
new Thread(bob).start();
new Thread(fred).start();
}
#Override
public void onRaceFinished(String finisher) {
synchronized (this) {
if (winner == null) {
winner = finisher;
System.out.println(finisher + " is the winner!");
} else {
System.out.println(finisher + " lost.");
}
}
}
}
Output
Tortoise is the winner!
Hare lost.
After this line:
}while(bob.getTotalDistance() < winDistance && fred.getTotalDistance() < winDistance);
You would just have:
boolean bobWins = (bob.getTotalDistance() >= winDistance);
boolean fredWins = (fred.getTotalDistance() >= winDistance);
if (bobWins && fredWins) {
System.out.println("It's a tie");
}
else if (bobWins) {
System.out.println("Bob Wins");
}
else {
System.out.println("Fred Wins");
}

How to consume in Producer-Consumer using Semphores?

I am trying out the Producer-Consumer problem using Semaphore. The program looks fine to me except for one place.
public class ProducerConsumerWithSemaphores
{
private final ArrayList<Integer> list = new ArrayList<>(5);
private final Semaphore semaphoreProducer = new Semaphore(1);
private final Semaphore semaphoreConsumer = new Semaphore(0);
private void produce() throws InterruptedException
{
for(int i = 0;i< 5;i++)
{
semaphoreProducer.acquire();
list.add(i);
System.out.println("Produced: " + i);
semaphoreConsumer.release();
}
}
private void consumer() throws InterruptedException
{
while (!list.isEmpty()) /// This line is where I have the doubt
{
semaphoreConsumer.acquire();
System.out.println("Consumer: " + list.remove(list.size()-1));
semaphoreProducer.release();
Thread.sleep(100);
}
}
public static void main(String[] args)
{
final ProducerConsumerWithSemaphores obj = new ProducerConsumerWithSemaphores();
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
obj.produce();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
obj.consumer();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}).start();
}
}
Is it okay to check the list if it is not empty before acquiring the semaphore? Will this cause any problem in multithreaded environment?
private void consumer() throws InterruptedException
{
while (!list.isEmpty()) /// This line is where I have the doubt
The problem is, if consumer runs faster than producer, your consumer quit immediately, then you have no consumer!!
The correct example looks like,
Producer–consumer problem#Using semaphores. I believe your intention is not to use true as endless loop because you want Producer/Consumer to quit when job is done. If that's your intention, you can 1. set a totalCount to end the loop. 2. Or a boolean flag which will be set by producer after putItemIntoBuffer when producer put the last one. The flag must be protected as well as the buffer.(update: this method doesn't work if there's multiple producers/consumers) 3. Simulate EOF ( idea taken from producer - consume; how does the consumer stop?)
Will this cause any problem in multithreaded environment?
Your critical section (your list) is not protected . Usually we use 3 semaphores. The 3rd one is used as a mutex to protect the buffer.
To stop producers/consumers,
Example code with method 1:
public class Test3 {
private Semaphore mutex = new Semaphore(1);
private Semaphore fillCount = new Semaphore(0);
private Semaphore emptyCount = new Semaphore(3);
private final List<Integer> list = new ArrayList<>();
class Producer implements Runnable {
private final int totalTasks;
Producer(int totalTasks) {
this.totalTasks = totalTasks;
}
#Override
public void run() {
try {
for (int i = 0; i < totalTasks; i++) {
emptyCount.acquire();
mutex.acquire();
list.add(i);
System.out.println("Produced: " + i);
mutex.release();
fillCount.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
private final int totalTasks;
Consumer(int totalTasks) {
this.totalTasks = totalTasks;
}
#Override
public void run() {
try {
for (int i = 0; i < totalTasks; i++) {
fillCount.acquire();
mutex.acquire();
int item = list.remove(list.size() - 1);
System.out.println("Consumed: " + item);
mutex.release();
emptyCount.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void runTest() {
int numProducer = 3;
int tasksPerProducer = 10;
int numConsumer = 6;
int tasksPerConsumer = 5;
for (int i = 0; i < numProducer; i++) {
new Thread(new Producer(tasksPerProducer)).start();
}
for (int i = 0; i < numConsumer; i++) {
new Thread(new Consumer(tasksPerConsumer)).start();
}
}
public static void main(String[] args) throws IOException {
Test3 t = new Test3();
t.runTest();
}
}
Example code with method 3:
public class Test4 {
private Semaphore mutex = new Semaphore(1);
private Semaphore fillCount = new Semaphore(0);
private Semaphore emptyCount = new Semaphore(3);
private Integer EOF = Integer.MAX_VALUE;
private final Queue<Integer> list = new LinkedList<>(); // need to put/get data in FIFO
class Producer implements Runnable {
private final int totalTasks;
Producer(int totalTasks) {
this.totalTasks = totalTasks;
}
#Override
public void run() {
try {
for (int i = 0; i < totalTasks + 1; i++) {
emptyCount.acquire();
mutex.acquire();
if (i == totalTasks) {
list.offer(EOF);
} else {
// add a valid value
list.offer(i);
System.out.println("Produced: " + i);
}
mutex.release();
fillCount.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
#Override
public void run() {
try {
boolean finished = false;
while (!finished) {
fillCount.acquire();
mutex.acquire();
int item = list.poll();
if (EOF.equals(item)) {
// do not consume this item because it means EOF
finished = true;
} else {
// it's a valid value, consume it.
System.out.println("Consumed: " + item);
}
mutex.release();
emptyCount.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void runTest() {
int numProducer = 3;
int tasksPerProducer = 10;
for (int i = 0; i < numProducer; i++) {
new Thread(new Producer(tasksPerProducer)).start();
}
int numConsumer = numProducer; // producers will put N EOFs to kill N consumers.
for (int i = 0; i < numConsumer; i++) {
new Thread(new Consumer()).start();
}
}
public static void main(String[] args) throws IOException {
Test4 t = new Test4();
t.runTest();
}
}
Instead of using two semaphores why dont you use a single semaphore to such that the synchronization is made between threads link
Additional you can use ArrayBlockingQueue which are thread safe to properly demonstrate the Producer Consumer Problem.

GUI timer does not stop

I have a problem with my GUI timer. I start my timer when the player clicks on a button in the mine field, and I stop my timer when the game ends.
The first time I test my game, the timer starts and ends normally. However, if I try to play a second round of the game, my timer does not stop.
public class Minesweeper7 extends JFrame implements ActionListener {
static public boolean revealed [][];
public static CountTimer ct;
public Minesweeper7 (int row, int column) {
ct = new CountTimer ();
}
private void setTimerText (String sTime) {
timeLabel.setText (sTime);
}
public void actionPerformed(ActionEvent event){
String coords = event.getActionCommand();
String[] squarePressed = coords.split(",");
x_pressed = Integer.parseInt(squarePressed[0]);
y_pressed = Integer.parseInt(squarePressed[1]);
System.out.println("The button you have pressed is " + x_pressed + ", " + y_pressed);
if (ct.timer.isRunning() == false){
ct.start ();
System.out.println ("timer is running");
}
reveal (row,column,x_pressed,y_pressed,revealed,mines,revealed_number);
gameEnd (row, column, revealed, mines, countMines, counter, end);
System.out.println("There are " + countMines + " .");
System.out.println("");
for (int r = 0;r<row;r++){
for (int c = 0;c<column;c++){
label[r][c].setText(String.valueOf(revealed_number[r][c]));
}
}
}
public void reveal () {
//a recursive method that reveals my buttons
}
public static void main (String args []) {
java.awt.EventQueue.invokeLater (new Runnable () {
public void run () {
new Minesweeper7 (10, 10);
}
});
}
public void gameEnd (int row, int column, boolean revealed [][], boolean mines [][], int countMines, int counter, boolean end) {
for (int r = 0; r < row; r++) {
for (int c = 0; c < column; c++) {
if (mines [r][c] == false && revealed [r][c] == true) {
counter++;
}
else if (mines [r][c] == true && revealed [r][c] == true) {
System.out.println ("Sorry, you lost because you clicked on a mine.");
end = true;
break;
}
}
}
if (counter == (row*column-countMines)) {
System.out.println ("Congratulations! You won the game!");
end = true;
instruction.setText("Congratulations! You won the game!");
}
if (end == true) {
ct.stop ();
}
}
public class CountTimer implements ActionListener {
public static final int ONE_SECOND = 1000;
public int count = 0;
public boolean isTimerActive = false;
public Timer timer = new Timer (ONE_SECOND, this);
public CountTimer () {
count = 0;
setTimerText (TimeFormat (count));
}
public void actionPerformed (ActionEvent e) {
if (isTimerActive) {
count++;
setTimerText (TimeFormat (count));
}
}
public void start () {
count = 0;
isTimerActive = true;
timer.start ();
}
public void stop () {
timer.stop ();
}
public void reset () {
count = 0;
isTimerActive = true;
timer.restart ();
}
}
}
if you start a new game, you could just make a new timer instead of starting and stopping.
if (game end) {
timer = new Timer();
timer.start();
}

Parallel task with synchronized arraylist

My homework was to create project using parallelization that all should be proper.
However, I made my project but my profesor mentioned something is wrong in my code "please look at array list, something is not ok, maybe synchronization?".
I would like ask you community to help me and point what could be wrong. I think it might be problem with not covering by synchronize brackets my array list, am I right?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* My project finds all dividors for specific number
*It must use threads, so I made them. First I start them (first loop)
*then join them (second loop). My project must have that loops.
*Problem might be with not synchronizing methods array list...
*/
public class Main {
private final static int NUMBER = 100;
private final static List<Integer> dividors = new ArrayList<Integer>();
public static void main(String[] args) {
new Main().doStuff();
}
private int sqr;
private int sqrp1;
private void doStuff() {
sqr = (int) Math.sqrt(NUMBER);
sqrp1 = sqr + 1;
Thread[] t = new Thread[sqrp1];
//starting tasks
for (int i = 1; i < sqrp1; i++) {
final int it = i;
if (NUMBER % i == 0) {
final int e = i;
t[i] = new Thread(new Runnable() {
#Override
public void run() {
System.out.println("sta"+e);
if (!checkContains(e)) {
addElement(e);
}
final int dividednumber = NUMBER / e;
if (!checkContains(dividednumber)) {
addElement(dividednumber);
}
}
});
t[i].start();
}
}
//calling join for tasks
for (int i = 1; i < sqrp1; i++) {
final int it = i;
if (NUMBER % i == 0) {
try {
System.out.println("sto"+i);
t[i].join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("xxx");
Collections.sort(dividors);
Integer[] arrayDividors = dividors.toArray(new Integer[0]);
for (int i = 0; i < arrayDividors.length; i++) {
System.out.println(arrayDividors[i]);
}
}
private synchronized void addElement(int element) {
dividors.add(element);
}
private synchronized boolean checkContains(int element) {
return dividors.contains(element);
}
}
Am I right changing this part, is it ok now?
t[i] = new Thread(new Runnable() {
#Override
public void run() {
System.out.println("waiting " + e);
synchronized (this) {
System.out.println("entering " + e);
if (!checkContains(e)) {
addElement(e);
}
final int dividednumber = NUMBER / e;
if (!checkContains(dividednumber)) {
addElement(dividednumber);
}
System.out.println("leaving " + e);
}
}
});
You need to turn this into a single atomic operation.
if (!checkContains(dividednumber)) {
addElement(dividednumber);
}
Imagine you have two threads.
T1: if (!checkContains(dividednumber)) { // false
T2: if (!checkContains(dividednumber)) { // false
T1: addElement(dividednumber); // adds number
T2: addElement(dividednumber); // adds same number
If you have one addElementWithoutDuplicates, this won't happen.

Categories