Jump to content

I need the thread to multitask.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Lawtonfogle

Lawtonfogle

    Newbie

  • Members
  • Pip
  • 8 posts
The point of threads are to allow a program to mulittask (at least in some cases), but I am needing a thread to multitask.

This is a part of my client and server chat server I have been making for scratch, just to know how to operate a server.

One server side thread gets stuck on this line of code.

command = in.readLine();

waiting for the BufferedReader, aka in, to get a comand from the client.

Of course, once it gets the command, I have a normal thread sleep.

What I am wondering is while I am waiting for something to be present to be read, is there a way to turn the waiting on and off?

#2
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts

Lawtonfogle said:

What I am wondering is while I am waiting for something to be present to be read, is there a way to turn the waiting on and off?
Please specify ...
Do you mean the waiting after the line is read or do you mean the waiting for that line itself?
In case the sleep/wait after that - just put a boolean variable and if:

...

   if( shouldSleepAfterRead)

       Thread.sleep(...);

...

set shouldSleepAfterRead from some other thread ( the current is blocked in the reading ).

In case you need to stop the readLine() command - read about interrupt().
You can interrupt a thread:
Thread (Java 2 Platform SE 5.0)
Thread (Java 2 Platform SE 5.0)
Thread (Java 2 Platform SE 5.0)

Hope this helps ...:)