freelanceprogrammers.org Forum Index » Java
Thread question
Joined: 17 Apr 2004
Posts: 28
Thread question
In my ally class, I have an int moveType and then I have a thread whose
run method contains a switch for the moveType integer. For example, an
ally advances, receives his/her orders and then retreats. Each ally
contains a thread for movement. As far as I know, there can only be one
run method for each class - that`s why I set up the switch and the
moveType integer. What I need to know is if I were to issue an
ally.advance() followed immediately by an ally.retreat(), how would I
make sure that the retreat was executed after the advance was complete?
I checked the API specs for class Thread and they mentioned a
Thread.join() method, but I`m not sure if I need to put it in a loop or
what. Can anyone tell me how to do this? Thanks.
-Michael Sullivan-
Joined: 10 Apr 2004
Posts: 19
Thread question
You could synchronize on a particular section of code that controls advancing
and retreating. If the retreat call is made, it will check if the advance is
finished, if not, it will be put into a wait. If the advance call is made and
completes, it will set whatever variable you have to indicate the advance is
complete and then call notify or notifyAll to wake up the threads that are
waiting on that piece of synchronized code. Sun`s site should have some pretty
good tutorials on threading if you need more information.
Dustin
Michael Sullivan <michael@...> wrote:
In my ally class, I have an int moveType and then I have a thread whose
run method contains a switch for the moveType integer. For example, an
ally advances, receives his/her orders and then retreats. Each ally
contains a thread for movement. As far as I know, there can only be one
run method for each class - that`s why I set up the switch and the
moveType integer. What I need to know is if I were to issue an
ally.advance() followed immediately by an ally.retreat(), how would I
make sure that the retreat was executed after the advance was complete?
I checked the API specs for class Thread and they mentioned a
Thread.join() method, but I`m not sure if I need to put it in a loop or
what. Can anyone tell me how to do this? Thanks.
-Michael Sullivan-
---------------------------------
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
[Non-text portions of this message have been removed]
Joined: 17 Apr 2004
Posts: 28
Thread question
I`m not sure I understand. I`ve never used threads or synchronized
methods before, so I have no idea what I`m doing. I tried the
notifyAll() method, but since I don`t know who owns the monitor, It kept
giving me IllegalMonitorStateException s on my notifyAll() line. I
can`t figure out how to call it. Can you give me an example? I`ve
pasted my code for advance(), retreat(0) and run() below. The moveType
variable is a constant integer, as is waitFlag, but waitFlag is only
until I figure out another way... Thanks.
-Michael Sullivan-
public synchronized void advance()
{
if (thread.isAlive())
{
waitFlag = ADVANCE;
moveType = WAIT;
waiting.start();
}
else
{
moveType = ADVANCE;
thread.start();
}
}
public synchronized void retreat()
{
if (thread.isAlive())
{
waitFlag = RETREAT;
moveType = WAIT;
waiting.start();
}
else
{
moveType = RETREAT;
thread.start();
}
}
public void run()
{
switch (moveType)
{
case ADVANCE:
while (getXPos() > 350)
{
moveLeft(20);
setCurrentImage(WALK);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
setCurrentImage(STAND);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
waiting.notifyAll();
break;
case RETREAT:
while (getXPos() <= 500)
{
moveRight(20);
setCurrentImage(WALK);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
setCurrentImage(STAND);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
break;
case WAIT:
moveType = waitFlag;
try { waiting.wait(); } catch (InterruptedException e) {}
waiting.start();
}
}
On Mon, 2004-04-26 at 17:14, Dustin Metzgar wrote:
> You could synchronize on a particular section of code that controls advancing
and retreating. If the retreat call is made, it will check if the advance is
finished, if not, it will be put into a wait. If the advance call is made and
completes, it will set whatever variable you have to indicate the advance is
complete and then call notify or notifyAll to wake up the threads that are
waiting on that piece of synchronized code. Sun`s site should have some pretty
good tutorials on threading if you need more information.
>
> Dustin
>
> Michael Sullivan <michael@...> wrote:
> In my ally class, I have an int moveType and then I have a thread whose
> run method contains a switch for the moveType integer. For example, an
> ally advances, receives his/her orders and then retreats. Each ally
> contains a thread for movement. As far as I know, there can only be one
> run method for each class - that`s why I set up the switch and the
> moveType integer. What I need to know is if I were to issue an
> ally.advance() followed immediately by an ally.retreat(), how would I
> make sure that the retreat was executed after the advance was complete?
> I checked the API specs for class Thread and they mentioned a
> Thread.join() method, but I`m not sure if I need to put it in a loop or
> what. Can anyone tell me how to do this? Thanks.
>
> -Michael Sullivan-
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Photos: High-quality 4x6 digital prints for 25
>
> [Non-text portions of this message have been removed]
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
Joined: 10 Apr 2004
Posts: 19
Thread question
Here`s some simple code I wrote up that you can take a look at. I`m not exactly
sure where you`re getting some of the ideas that you`re using, so I`ll present
synchronizing on data in its simplest form:
public class MyThreadTest {
public boolean hasAdvanced = false;
public synchronized void advance() {
System.out.println("advance method started");
if (hasAdvanced == false)
hasAdvanced = true;
this.notifyAll();
System.out.println("advance method finished");
}
public synchronized void retreat() {
System.out.println("retreat method started");
while (hasAdvanced == false)
{
try
{
this.wait();
}
catch (InterruptedException e) {}
}
System.out.println("retreat method finished");
}
public static void main(String[] args) {
MyThreadTest t = new MyThreadTest();
Thread t1 = new MyThread1(t);
t1.start();
Thread t2 = new MyThread2(t);
t2.start();
}
}
class MyThread1 extends Thread {
private MyThreadTest t;
public MyThread1(MyThreadTest t) { this.t = t; }
public void run() {
t.retreat();
}
}
class MyThread2 extends Thread {
private MyThreadTest t;
public MyThread2(MyThreadTest t) { this.t = t; }
public void run() {
t.advance();
}
}
Dustin
Michael Sullivan <michael@...> wrote:
I`m not sure I understand. I`ve never used threads or synchronized
methods before, so I have no idea what I`m doing. I tried the
notifyAll() method, but since I don`t know who owns the monitor, It kept
giving me IllegalMonitorStateException s on my notifyAll() line. I
can`t figure out how to call it. Can you give me an example? I`ve
pasted my code for advance(), retreat(0) and run() below. The moveType
variable is a constant integer, as is waitFlag, but waitFlag is only
until I figure out another way... Thanks.
-Michael Sullivan-
public synchronized void advance()
{
if (thread.isAlive())
{
waitFlag = ADVANCE;
moveType = WAIT;
waiting.start();
}
else
{
moveType = ADVANCE;
thread.start();
}
}
public synchronized void retreat()
{
if (thread.isAlive())
{
waitFlag = RETREAT;
moveType = WAIT;
waiting.start();
}
else
{
moveType = RETREAT;
thread.start();
}
}
public void run()
{
switch (moveType)
{
case ADVANCE:
while (getXPos() > 350)
{
moveLeft(20);
setCurrentImage(WALK);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
setCurrentImage(STAND);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
waiting.notifyAll();
break;
case RETREAT:
while (getXPos() <= 500)
{
moveRight(20);
setCurrentImage(WALK);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
setCurrentImage(STAND);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
break;
case WAIT:
moveType = waitFlag;
try { waiting.wait(); } catch (InterruptedException e) {}
waiting.start();
}
}
---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
[Non-text portions of this message have been removed]
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







