Results 1 to 7 of 7
  1. #1
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default forcing a thread to run

    Is there any way to guarantee a method call within a thread will complete? I am working on a chess program, and I am at the end of it.
    The problem is I have a thread called ClockThread that keeps track of time. When time runs out, this thread calls the games reset method which resets the board, however, it doesnt do it right away. Im pretty sure it because the thread is no longer running and it waits to run for a bit before finally resetting the board. Is there a way to guarantee the call to reset() finishes?

  2. #2
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: forcing a thread to run

    Most probably your thread implementation is wrong
    I have to see your code that thread runs and reset() method

    if you put a thread in a infinite loop then it waits forever untill the trigger of that thread is alive.
    Thread structure is tree so when parent thread dies,it will die as well.

  3. #3
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: forcing a thread to run

    ok, i will post the code now. i dont think the implementation is wrong, but it could be. the reason i say this is if you are playing as white, the board will reset, it will just take a minute or two. if you are playing as black, the board resets only after white has moved and you try to make a move. you have to make this move blind however because the baord will be in its old state. could it have something to do with the event dispatch thread? im not too sure how that works

    thread run method:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.sql.*;
    import java.io.*;
    
    public class ClockThread extends Thread {
    	private JPanel clock;
    	private GameFrame gameFrame;
    	private long counter;
    	private long init_counter;
    	private MoveListener listener;
    	private int side;
    	private Writer output;
    	
    	public ClockThread(JFrame game, JPanel clock, long counter, MoveListener listener, int side, Writer output) {
    		this.clock = clock;
    		gameFrame = (GameFrame)game;
    		this.listener = listener;
    		this.counter = counter;
    		init_counter = counter;
    		this.side = side;
    		this.output = output;
    	}
    	
    	public void run() {
    		long currentTime = System.currentTimeMillis();
    		long tmpTime;
    		long extra = 0;
    		
    		while(true) {
    			tmpTime = System.currentTimeMillis() - currentTime + extra;
    			
    			if(tmpTime > 1000 && side == listener.getTurn()) {
    				currentTime = System.currentTimeMillis();
    				counter -= 1000;
    				//extra = tmpTime-1000;
    				
    				if(counter <= 0) {
    					try {
    						output.write("g\r\n");
    						output.flush();
    					}
    					catch(IOException e) {
    						System.out.println("transmit error: " + e);
    					}
    					gameFrame.gameOver(gameFrame.getSide());
    					continue;
    				}
    				
    				clock.removeAll();
    				int hours = (int)counter/60/60000;
    				int minutes = (int)(counter/60000)-hours*60;
    				int seconds = (int)(counter)/1000 - minutes*60 - hours*3600;
    				String timeString = hours+":"+minutes+":"+seconds;
    				clock.add(new JLabel(timeString));
    				clock.revalidate();
    				gameFrame.setTime(counter);
    			}
    		}
    	}
    	
    	public void setTime(long c) {
    		counter = c;
    	}
    	
    	public long getTime() {
    		return counter;
    	}
    
    }

    reset method:
    Java Code:
    public void reset() {
    		//clockThread.stop();
    		Board board = game.getBoard();
    		
    		for(int i = 0; i < 8; ++i) {
    			for(int j = 0; j < 8; ++j) {
    				squares[j][i].setResident(null);
    				squares[j][i].repaint();
    				squares[j][i].setResident(board.getSquare(j,i).getResident());
    				squares[j][i].revalidate();
    			}
    		}
    		layeredPane.revalidate();
    		capturedWhite.removeAll();
    		capturedBlack.removeAll();
    		int turn = listener.getTurn();
    		
    		if(turn == Colors.BLACK)
    			listener.changeTurn();
    			
    		//clockThread =  new ClockThread(this, clockPanel, startTime, listener, side, output);
    		clockThread.setTime(startTime);
    		//clockThread.start();
    	}
    	
    	public  void gameOver(int color) {
    		if(color == Colors.WHITE) {
    			new ErrorDialog(this, "Game over: Black wins");
    			game.reset();
    			this.reset();
    		}
    		if(color == Colors.BLACK) {
    			new ErrorDialog(this, "Game over: White wins");
    			game.reset();
    			this.reset();
    		}		
    	}

  4. #4
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: forcing a thread to run

    Your code is a little complicated.

    you calls gameOver() method.
    Your problem is right there. What is ErrorDialog? if you initializes it then ErrorDialog gets the thread and uses untill constructor finishes its job.
    (I don't think that ErroDialog is GUI. If it is GUI you have to use another thread to run because it blocks thread);

    If it is not the case that means game.reset() method kills the clockThread.

    Hint: you can join threads and waits another thread to get results of clockThread.
    ThreadGroup.....

  5. #5
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: forcing a thread to run

    ErrorDialog is GUI. Do you think thats the problem? However, it has the property DISPOSE_ON_CLOSE, so once it is closed, shouldnt the thread continue? All it does it display who won, and after that the user should close it. I dont think game.reset kills the clock thread, because the game and the clock will continue, just not right away. For white, it takes a minute or two and then the reset finishes, the clock resets, and it starts counting down again. for black, rest is a bit more complicated and it needs some input from the user to finish, thats why i asked about the even dispatch thread.

  6. #6
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: forcing a thread to run

    If you do not think it is okay for me.
    But at least you can trigger new thread to check
    You can see the result.

    Thread t1=new Thread(){
    @Override
    public void run(){

    //Run code here
    }

    };
    t1.start();

  7. #7
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: forcing a thread to run

    there 2 type of GUI
    launch
    invokeANDWAIT
    invokeLATER

    Applet and Other (Window(Frame))

    for more information:>
    http://download.oracle.com/javase/7/...ventQueue.html
    Last edited by Grkn; 11-03-2011 at 07:38 PM.

Similar Threads

  1. Main Thread not waiting for grand child thread to finish
    By prashanthn in forum Threads and Synchronization
    Replies: 3
    Last Post: 06-07-2011, 10:26 AM
  2. Forcing the mouse to stay quiet
    By ProspectiveDeveloper in forum AWT / Swing
    Replies: 4
    Last Post: 05-03-2010, 12:17 AM
  3. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 03:13 AM
  4. Forcing a thread to stop
    By sukatoa in forum Threads and Synchronization
    Replies: 7
    Last Post: 07-17-2009, 07:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •