SYNCHRONIZING problem made simple

A

adrian.bartholomew

ok. let me try an easier explanation.
online card game for 4.

my main program on the server side is runnable. it is also an animated
gui of the game showing all 4 hands.
i have an endless loop as my run() method.
it consists of many if statements that each call various animation
methods that exist outside this run() method.
whenever an animation is needed, another thread turns one of these
flags "true".
the run() method then goes into that particular animation method. the
1st line of code of the animation immediately switches the flag back
to "false" to prevent false re-entry.

is there another way to do this animation?
the resources of the server computer would be bogged down if each
instance of the game ran an endless loop no?

more importantly, i need each animation method to be atomized but when
i synchronize them, the other thread that carries out client requests
goes into deadlock.
if i have to, how do i use wait() notifyAll() in this situation?
 
K

Knute Johnson

ok. let me try an easier explanation.
online card game for 4.

my main program on the server side is runnable. it is also an animated
gui of the game showing all 4 hands.
i have an endless loop as my run() method.
it consists of many if statements that each call various animation
methods that exist outside this run() method.
whenever an animation is needed, another thread turns one of these
flags "true".
the run() method then goes into that particular animation method. the
1st line of code of the animation immediately switches the flag back
to "false" to prevent false re-entry.

is there another way to do this animation?

Probably but your idea sounds OK for now.
the resources of the server computer would be bogged down if each
instance of the game ran an endless loop no?

Depends on a lot of things but you have control over all of them so I
wouldn't worry too much about that either.
more importantly, i need each animation method to be atomized but when
i synchronize them, the other thread that carries out client requests
goes into deadlock.
if i have to, how do i use wait() notifyAll() in this situation?

Keep a reference to the original method's thread and interrupt it if it
is still running. Then you don't even have to synchronize them.
 
A

adrian.bartholomew

Probably but your idea sounds OK for now.


Depends on a lot of things but you have control over all of them so I
wouldn't worry too much about that either.


Keep a reference to the original method's thread and interrupt it if it
is still running. Then you don't even have to synchronize them.

thanks knute. thats what i was using b4. but i was using
threadName.suspend() and threadName.resume()
i had ultimate control but i did ran into some minor problems. seeing
that these methods were deprecated anyway i thought y not opt for a
more modern model. the "synchronized" approach. no?
 
K

Knute Johnson

thanks knute. thats what i was using b4. but i was using
threadName.suspend() and threadName.resume()
i had ultimate control but i did ran into some minor problems. seeing
that these methods were deprecated anyway i thought y not opt for a
more modern model. the "synchronized" approach. no?

Well what I think you want to do is start and stop an assortment of
animation threads, running only one at a time. If that is correct then
I think you should do as I suggest. Start a new thread running your
animation and when you want to run a different one, interrupt the thread
and then start a new one. Don't use Thread.resume() or Thread.suspend()
as they are deprecated and don't work. Look at Thread.interrupt() and
Thread.interrupted() or Thread.isInterrupted(). Also look at Thread.join().
 
A

adrian.bartholomew

Well what I think you want to do is start and stop an assortment of
animation threads, running only one at a time. If that is correct then
I think you should do as I suggest. Start a new thread running your
animation and when you want to run a different one, interrupt the thread
and then start a new one. Don't use Thread.resume() or Thread.suspend()
as they are deprecated and don't work. Look at Thread.interrupt() and
Thread.interrupted() or Thread.isInterrupted(). Also look at Thread.join().

u mean like....inner class run methods?
 
K

Knute Johnson

u mean like....inner class run methods?

Here is one way to do it. There are numerous other possibilities. I
like this approach because it encapsulates the threads in their own
classes since they might all be slightly different.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame implements ActionListener {
Thread animationThread;

public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

JButton b = new JButton("One");
b.addActionListener(this);
add(b);

b = new JButton("Two");
b.addActionListener(this);
add(b);

b = new JButton("Stop");
b.addActionListener(this);
add(b);

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("One")) {
if (animationThread != null)
animationThread.interrupt();
One one = new One();
animationThread = new Thread(one);
animationThread.start();
} else if (ac.equals("Two")) {
if (animationThread != null)
animationThread.interrupt();
Two two = new Two();
animationThread = new Thread(two);
animationThread.start();
} else if (ac.equals("Stop")) {
if (animationThread != null)
animationThread.interrupt();
}
}

class One implements Runnable {
public void run() {
while (!Thread.interrupted()) {
System.out.println("One");
}
}
}

class Two implements Runnable {
public void run() {
while (!Thread.interrupted()) {
System.out.println("Two");
}
}
}

public static void main (String[] args) {
Runnable r = new Runnable() {
public void run() {
new test();
}
};
EventQueue.invokeLater(r);
}
}
 
A

adrian.bartholomew

u mean like....inner class run methods?

Here is one way to do it. There are numerous other possibilities. I
like this approach because it encapsulates the threads in their own
classes since they might all be slightly different.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame implements ActionListener {
Thread animationThread;

public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

JButton b = new JButton("One");
b.addActionListener(this);
add(b);

b = new JButton("Two");
b.addActionListener(this);
add(b);

b = new JButton("Stop");
b.addActionListener(this);
add(b);

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("One")) {
if (animationThread != null)
animationThread.interrupt();
One one = new One();
animationThread = new Thread(one);
animationThread.start();
} else if (ac.equals("Two")) {
if (animationThread != null)
animationThread.interrupt();
Two two = new Two();
animationThread = new Thread(two);
animationThread.start();
} else if (ac.equals("Stop")) {
if (animationThread != null)
animationThread.interrupt();
}
}

class One implements Runnable {
public void run() {
while (!Thread.interrupted()) {
System.out.println("One");
}
}
}

class Two implements Runnable {
public void run() {
while (!Thread.interrupted()) {
System.out.println("Two");
}
}
}

public static void main (String[] args) {
Runnable r = new Runnable() {
public void run() {
new test();
}
};
EventQueue.invokeLater(r);
}

}

it works!
i have some rearranging to do though and i can do away with the main
run routine entirely.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top