[Java] Pausing a while loop

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

[Java] Pausing a while loop

Post by Wesley »

Okay, I have a game called Pig I've been working on. It's a dice game that has human players and computer players. When a computer player goes, it simply rolls the dice until it is told to stop (like if it loses the round). I want to set a delay for each time the computer rolls the dice so it doesn't simply go 10 times before passing the dice.

I've tried Thread.sleep(delay), but then the GUI doesn't update the information (like the current roll on the dice). I conducted an experiment and found that if I use a JOptionPane to make the pause, the GUI updates perfectly.

I want to make the program produce the same results, but without the JOptionPane being present.

Any ideas?

Oh, I also tried using a Timer, but it produced the same results as the Thread.sleep().

Here's the code for the computer:

Code: Select all

if (comp) {
	while (!pass) {
		if (computerScore+roundScore>=GOAL) {
			status = name+" has won the game!";
			update();
			end=true;
		} else {
			if (computerScore+roundScore>=humanScores+TURNOVER) {
				status = "The "+name+" has turned over the dice by default.";
				update();
				JOptionPane.showMessageDialog(null, status);
				status = "";
				pass=true;
			} else {
				rollDice();
				if (losePoints()) {
					computerScore = 0;
					roundScore = 0;
					pass=true;
				} else
					if (loseRound()) {
						roundScore = 0;
						pass=true;
					} else
						roundScore += points;
				//I want to use a Timer here, instead of JOptionPane.
				update();
				JOptionPane.showMessageDialog(null, name+" has rolled: "+toString());
				System.out.println(toString());
			}
		}
	}
}
ImageImage
ImageImage
Image
darkstone knight
New Member
Posts: 67
Joined: Sun 09 Nov, 2008 1:56 pm

Re: [Java] Pausing a while loop

Post by darkstone knight »

<---- yava noob

from another thread:
King Harold wrote:Same thing probably, but maybe you could just put a Sleep in it somewhere, that would help.. Just sleep for 1 milisecond or something that's already a whole lot (seeing as most instructions take a fraction of a nanosecond..)
solution: use sleep(delay)
?
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [Java] Pausing a while loop

Post by Wesley »

darkstone knight wrote:solution: use sleep(delay)
Ugh, I already explained that using a Thread won't work properly. Something is up with the graphics, they won't redraw when using sleep(delay).

Any other ideas?
ImageImage
ImageImage
Image
darkstone knight
New Member
Posts: 67
Joined: Sun 09 Nov, 2008 1:56 pm

Re: [Java] Pausing a while loop

Post by darkstone knight »

use a small delay (20ms) and use that repeatly
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Java] Pausing a while loop

Post by benryves »

It seems that you are pausing the UI thread, hence it locking up during the pause. Pause in a separate thread to the UI one and all should be good.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [Java] Pausing a while loop

Post by Wesley »

benryves wrote:It seems that you are pausing the UI thread, hence it locking up during the pause. Pause in a separate thread to the UI one and all should be good.
How do I pause in a separate thread? I don't know anything about threads. This project I'm working on is totally on my free time, not an assignment. Hence, I'm not learning (or haven't learned), in the classroom, about a lot of the things I'm implementing in the program.
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Java] Pausing a while loop

Post by benryves »

I don't know what assignments have to do with anything?

I am not familiar with Java's class library, I'm afraid. Program execution happens on threads, where only one thing can happen at a time, which is why sleeping the UI thread causes your app to freeze.

The Timer class looks ideal as that uses a background thread to schedule events.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [Java] Pausing a while loop

Post by Wesley »

Oh, I was just explaining that I don't understand the thread or UI information you were throwing out.

Hm... I also tried a Timer, but it didn't work. Maybe asking this question at the Sun forums would help :idea:.
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Java] Pausing a while loop

Post by benryves »

I suppose so. :( Sorry I couldn't be of any more help!
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [Java] Pausing a while loop

Post by Wesley »

Okay, I understand the threads more now. And I asked some questions on the Sun forums. Sadly, the people there aren't very nice and are VERY impatient. Worst of all, they couldn't answer my question. I wish I could speak with a Sun developer...

Anyway, I'm trying to figure out how to use Thread.sleep(delay) on all threads at once; or, at least, to target certain threads, so the function doesn't lock up the GUI thread only.

Ugh...
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Java] Pausing a while loop

Post by benryves »

You have to call Thread.sleep(delay) inside the thread you wish to sleep, as it only works on the currently executing thread.

The computer's turn is a sequence of operations. When it's the computer's turn to play, create a thread that runs this sequence of operations (roll dice, pause, make another turn &c - I don't know how the game works, sorry). I assume that at the moment when the computer plays the code runs directly alongside the code that handles the user interface - you need to take this game code and shunt it out into its own thread.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [Java] Pausing a while loop

Post by Wesley »

Ben, you are such a great help!

My only problem is trying to place the Thread.sleep(delay) method. I have been trying to place it in different places and have been testing, but the way my program works, I think I will have to edit it quite a bit just so I can get the delay to work properly.

Thanks again! I will work on it some more.
ImageImage
ImageImage
Image
Post Reply