|
-
Jul 18th, 2008, 05:18 PM
#1
Thread Starter
Frenzied Member
ComputerJy, need your expert help on threads please
Code:
public class Test {
public static void main(String[] args) {
GamePlayer game = new GamePlayer();
System.out.println(game.doSomeMove());
}
}
class GamePlayer {
int doSomeMove() {
// Cause some timeout on purpose.. 7 seconds!
Long stoptime = 7000L;
try {
Thread.sleep(stoptime);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 5;
}
}
class ThreadTesting implements Runnable {
public void start() {
}
public void stop() {
}
public void run() {
try {
}
catch (Exception e) {
}
}
}
I'm not too sure on how to do this.
as you see i deliberately make the doSomeMove() return the int after 7 seconds. i want to somehow run the doSomeMove() method in a thread and check it for timeout issues. If the method takes more than 5 seconds to respond, i want to stop the thread and print "player move took too long to repsond" otherwise i return the move (the int) as normal. can anyone help me with this
Last edited by Pouncer; Jul 18th, 2008 at 05:47 PM.
-
Jul 18th, 2008, 08:03 PM
#2
Re: ComputerJy, need your expert help on threads please
Code:
public class Test
{
static Integer returnedValue;
public static void main(String[] args)
{
final Thread workingThread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(7000);
returnedValue = 5;
}
catch (InterruptedException e)
{
}
}
});
final Thread checkingThread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(5000);
if (workingThread.isAlive() || returnedValue == null)
{
System.err.println("Timed out");
}
}
catch (InterruptedException e)
{
}
}
});
workingThread.start();
checkingThread.start();
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 19th, 2008, 04:47 AM
#3
Thread Starter
Frenzied Member
Re: ComputerJy, need your expert help on threads please
hey ComputerJy that is perfect and works good! but is there a way to have the working thread as a seperate class which returns 5?
-
Jul 19th, 2008, 05:53 AM
#4
Re: ComputerJy, need your expert help on threads please
No you can't, because a thread does not return any values
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 19th, 2008, 06:17 AM
#5
Thread Starter
Frenzied Member
Re: ComputerJy, need your expert help on threads please
I played around with this a bit, and got this done - its working fine
Code:
public class Test {
public static void main(String[] args) throws InterruptedException {
ThreadTesting tt = new ThreadTesting();
Thread t = new Thread(tt);
long patience = 2000;
long startTime = System.currentTimeMillis();
t.start();
while (t.isAlive()) {
// Keep checking if threads time goes over 'patience' seconds..
if (((System.currentTimeMillis() - startTime) > patience) && t.isAlive()) {
System.out.println("timeout......");
t.stop();
break;
}
if (tt.getValue() != null) {
System.out.println("Got a return value: " + tt.getValue());
t.stop();
break;
}
}
}
}
class GamePlayer {
int doSomeMove() {
// Cause some timeout on purpose.. 7 seconds!
try {
Thread.sleep(7000);
}
catch (InterruptedException e) { }
return 5;
}
}
class ThreadTesting implements Runnable {
Integer returnedValue;
public void start() {
}
public void stop() {
}
public void run() {
try {
GamePlayer gp = new GamePlayer();
returnedValue = gp.doSomeMove();
}
catch (Exception e) {
}
}
public Integer getValue() {
return returnedValue;
}
}
but what i need to do now is also print the time it took to return a value from doSomeMove() (if there wasnt a timeout..) any ideas mate?
-
Jul 19th, 2008, 01:37 PM
#6
Re: ComputerJy, need your expert help on threads please
Code:
import java.util.Date;
class GamePlayer
{
int doSomeMove()
{
// Cause some timeout on purpose.. 7 seconds!
try
{
Thread.sleep(7000);
}
catch (final InterruptedException e)
{
}
return 5;
}
}
public class Test
{
@SuppressWarnings("deprecation")
public static void main(final String[] args) throws InterruptedException
{
final ThreadTesting tt = new ThreadTesting();
final Thread t = new Thread(tt);
final long patience = 2000;
final long startTime = System.currentTimeMillis();
t.start();
while (t.isAlive())
{
// Keep checking if threads time goes over 'patience' seconds..
if (((System.currentTimeMillis() - startTime) > patience) && t.isAlive())
{
System.out.println("timeout......");
t.stop();
break;
}
if (tt.getValue() != null)
{
System.out.println("Got a return value: " + tt.getValue());
t.stop();
break;
}
}
}
}
class ThreadTesting implements Runnable
{
Integer returnedValue;
public Integer getValue()
{
return returnedValue;
}
public void run()
{
try
{
final GamePlayer gp = new GamePlayer();
Date d1 = new Date();
returnedValue = gp.doSomeMove();
Date d2 = new Date();
long val = d2.getTime() - d1.getTime();
System.out.println("Operation took " + val + " Milliseconds to complete");
}
catch (final Exception e)
{
}
}
public void start()
{
}
public void stop()
{
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|