exiting infinite wait of one thread with input from another in java - java

I am making a Java app which does something on a HotKey combination. I have an infinite while loop to wait for HotKey input to close, but it makes the app very CPU time costing.
Following is how my code looks in the simplest way:
static boolean isOpen = true;
void main()
{
....
add SomeHotKeyListener();
....
while(isOpen)
{ }
releaseResources();
}
void onHotKey(int hotKeyIdentifier)
{
if(hotKeyIdentifier == something)
do something;
if(hotKeyIdentifier == something)
isOpen = false;
}
I need a multi-threading approach to achieve this, or if someone has something better to fit in.

I recommend you read about the synchronized keyword in Java. Just Google it, and you should find a ton of examples and tutorials.
This should solve your case:
static boolean isOpen = true;
static Object lock = new Object();
void main()
{
....
add SomeHotKeyListener();
....
synchronized(lock)
{
while(isOpen)
{
try {
lock.wait()
} catch(InterruptedException e) {
}
}
}
releaseResources();
}
void onHotKey(int hotKeyIdentifier)
{
if(hotKeyIdentifier == something)
do something;
if(hotKeyIdentifier == something)
{
synchronized(lock)
{
isOpen = false;
lock.notify();
}
}
}

Infinite while loop can consume quite a lot of system resource. Using wait and notify is recommended. Also you have to declare your boolean volatile as otherwise there is no guarantee that the changes made by one thread is picked up by the other. Below is an example which does something in a separate thread and until interrupted by the calling thread based on a user input (an enter in this case). See also the example from Oracle here
import java.util.Scanner;
public class WaitTest implements Runnable {
private volatile boolean shutdown = false;
public static void main(String[] args) {
WaitTest w = new WaitTest();
new Thread(w).start();
System.out.println("Press any key to interrupt");
Scanner sc = new Scanner(System.in);
sc.nextLine();
w.triggerShutDown();
}
#Override
public void run() {
while (!shutdown) {
synchronized (this) {
try {
System.out.println("doing some silly things");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Server shutdown successfully");
}
public synchronized void triggerShutDown() {
this.shutdown = true;
notify();
}
}

Related

In java, How to access values of a class from other classes in thier differrent individual threads?

I have a class named "Clock" which have implementing Runnable. In run() a infinite loop is started where thread sleeps for 100ms for each iteration and then changes a boolean value :"isOk".
There is another class "ConOne" in its seperate thread also having infinite loop and it tries to get the "isOk" boolean value from "Clock" class. but if value is false then "ConOne" has to wait at the thread to continue.
So I created to ConOne objects trying to access the boolean value from "Clock" class.
But it thorws a Exception describing that "current object is not a owner of thread".
Why is this happening?
And sorry for my English.
Here's code:
Clock class
public class Clock implements Runnable {
boolean isOk;
Thread t;
Clock() {
isOk = false;
t = new Thread(this, "Clock_Thread");
}
void startClock() {
t.start();
}
public void run() {
int i = 0;
while(true) {
try {
t.sleep(100);
System.out.println("Tick:" + i);
if(isOk) {
isOk = false;
} else {
isOk = true;
notify();
}
i++;
} catch(InterruptedException ie) {
System.out.println("InterruptedException at Clock");
}
}
}
public boolean getPermit() {
if (!isOk) {
try {
wait();
} catch(InterruptedException e) {
System.out.println("Exception at clock.getPermit()");
}
}
return true;
}
}
ConOne class:
public class ConOne implements Runnable {
Thread t;
Clock ct;
ConOne(String name, Clock c) {
t = new Thread(this, name);
ct = c;
}
public void run() {
while(true) {
ct.getPermit();
repaint();
}
}
public void repaint() {
System.out.println("Repainted On " + t);
}
}
Class with main method:
public class Master {
public static void main(String[] args) {
Clock clock = new Clock();
ConOne con1 = new ConOne("Con11", clock);
ConOne con2 = new ConOne("Con12", clock);
clock.startClock();
con1.t.start();
con2.t.start();
}
}
Here is the error:
Error Screenshot
You can only call a wait on an object, if you have synchronized on that object.
So something like this:
synchronized(monitor){
while(!condition)
monitor.wait();
}
You’ve got things a bit twisted up I’m afraid, the notify() and wait() require ownership of the Clock object’s monitor. You can try to get the wait/notify semantics correct, but I recommend just using builtin tools, specifically a SynchronizedQueue. Clock can just hold one as a field and put 1 into it when isOk. The other thread can be put into a non-busy wait() with a simple take() from the queue, which will block until the Clock class puts something.

How can I safely stop my "class implements Runnable"?

The Oracle Java SE Docs recommend doing this:
You can avoid the use of Thread.stop by replacing the applet's stop and run methods with:
private volatile Thread blinker;
public void stop() {
blinker = null;
}
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
Thread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
Is there a way to do the same thing for a class blinker implements Runnable ?
As you would have to use blinker thisClass = this; or similar, wouldn't the (blinker == thisClass) always evaluate as true?
Or will this code suffice:
class blinker implements Runnable {
boolean stop = false;
#override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// code
// ...
if (stop) { Thread.currentThread().interrupt(); }
// ...
}
}
}
You could do something like that:
class Blinker implements Runnable {
Runnable blinker = this;
public void stop() {
blinker = null;
}
public void run() {
while(blinker == this) {
}
}
}
But it would be pretty pointless. I don't think you're understanding the point the documentation is trying to get across, which is don't use an infinite loop to keep threads alive, using Thread#stop() to terminate them. Instead, use a condition, then set it to false when you want to end the loop which is keeping the thread alive.
You do not need to constantly check Thread#isInterrupted() to keep the thread alive.
while(!stop) {
}
Would do just fine. You also should not interrupt the thread from within the thread. The purpose of interrupting is to end tasks that halt the thread. These tasks are surround within try/catch which catches an InterruptedException. Other threads are usually the ones in charge of interrupting.
The documentation is referring to allowing the thread to die gracefully.
In the first example, the run() method was handled via an infinite loop: while(true). The only way to stop the thread would be to forcing some kind of stop, such as usong Thread#stop:
public void run() {
while (true) {
try {
Thread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
But it's not recommended to use Thread#stop. Instead, the loop should depend on a boolean, which another thread (or the current one) could set to true or false:
private volatile boolean running;
public void stop() {
running = false;
}
public void run() {
while (running) {
try {
Thread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
Instead of using a running boolean, they used blinker == thisThread, then changed the value of blinker when they wanted to end the loop:
private volatile Thread blinker;
public void stop() {
blinker = null;
}
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
Thread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}

How can I start, pause and resume my threads? (by extending thread from classes)

Essentially, what I want to do is start all my threads, pause them all, then resume them all, using the multithreading approach. I am just looking for a simple solution to this. I'm not sure if I have to use a timer or what. Right now when I run it, the threads are like being executed in random order (I guess the PC is just randomly picking which ones it wants to run at a certain time).
class ChoppingThread extends Thread
{
public void run()
{
for(int j=40;j!=0;j-=10)
System.out.println("Chopping vegetables...("+j+" seconds left)");
}
}
class MixingThread extends Thread
{
public void run()
{
for(int k=60;k!=0;k-=10)
System.out.println("Mixing sauces...("+k+" seconds left)");
}
}
class TenderizingThread extends Thread
{
public void run()
{
for(int j=50;j!=0;j-=10)
System.out.println("Tenderizing meat...("+j+" seconds left)");
}
}
class MultiThreadTasking
{
public static void main (String [] args)
{
ChoppingThread ct = new ChoppingThread();
MixingThread mt = new MixingThread();
TenderizingThread tt = new TenderizingThread();
System.out.println("\nWelcome to the busy kitchen.");
//putting threads into ready state
ct.start();
mt.start();
tt.start();
}
}
There are probably other ways to achieve the same result, but this is the simplest I can come up with off the top of my head (I know, sad isn't it)...
Basically, this is a special Runnable with some additional management functionality.
This basically contains a state flag that indicates the state of the task and a monitor lock
public class ThreadFun {
public static void main(String[] args) {
MyTask task = new MyTask();
Thread thread = new Thread(task);
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
task.pauseTask();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
task.resumeTask();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
task.stopTask();
}
public enum TaskState {
Running,
Stopped,
Paused
}
public static class MyTask implements Runnable {
private static final Object PAUSED_LOCK = new Object();
private volatile TaskState state = TaskState.Running;
public void pauseTask() {
if (state == TaskState.Running) {
System.out.println("Paused...");
state = TaskState.Paused;
}
}
public void resumeTask() {
if (state == TaskState.Paused) {
state = TaskState.Running;
synchronized (PAUSED_LOCK) {
PAUSED_LOCK.notifyAll();
}
System.out.println("Resumed...");
}
}
public void stopTask() {
if (state == TaskState.Running || state == TaskState.Paused) {
state = TaskState.Stopped;
System.out.println("Stopped...");
}
}
public boolean isStopped() {
return state == TaskState.Stopped;
}
public boolean isPaused() {
return state == TaskState.Paused;
}
protected void doPause() {
synchronized (PAUSED_LOCK) {
while (isPaused()) {
try {
PAUSED_LOCK.wait();
} catch (InterruptedException ex) {
}
}
}
}
#Override
public void run() {
int index = 0;
while (!isStopped() && index < 1000) {
try {
Thread.sleep(25);
} catch (InterruptedException ex) {
}
doPause();
index++;
System.out.println(index);
}
stopTask(); // Make sure the task is marked as begin stopped ;)
}
}
}
The main criteria is you will need to pool isStopped and doPause at appropriate points to ensure that they are begin implemented as required...
To coordinate them use a CyclicBarrier.
To launch them all at the same time use a CountDownLatch.
Google the two classes above for many examples and explanations.
To fully understand what is happening read the Java Concurrency In Practice book.
I believe you can accomplish this by using Object.wait and Thread.interrupt.
Object.wait blocks until notify is called. So
private boolean paused;
private Object waitObject;
...
public void run() {
for ... {
if (this.paused) { this.waitObject.wait(); }
...
public void pause() { this.paused = true; }
public void resume() { this.paused = false; this.waitObject.notify(); }
Then you can call pause to pause the thread.
Thread.interrupt can help with stopping.
private boolean paused;
...
public void run() {
for ... {
// interrupted() is different from interrupt()!
if (this.iterrupted()) { break; }
...
To stop it, you would call interrupt() from another thread.
This is the basic idea, but there's a lot of details to worry about here. For example, wait can throw an InterruptedException you'll need to handle. Also, wait is not guaranteed to return only after a notify. It can return randomly. Here is a pair of tutorials:
Wait: http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html
Interrupt: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

Do I correctly shutdown these simultaneous threads

As in the subject do I correctly shutdown these simultaneous threads?
I assigned a volatile field and check it repeatedly in while loop.
Is there alternative way to do it(like using synchronize or wait() method), please show me.
EDIT I edited code. Is there any way of checking if Thread is alive by different method thatn isAlive();?
Perhaps:
boolean isAlive(){
return running;
}
import javax.swing.JOptionPane;
public class Wat extends Thread {
private char c;
private int interv;
private volatile boolean running = true;
Object synchObj;
public Wat(char c, int interv) {
this.c = c;
this.interv = interv;
synchObj = new Object();
}
public void run() {
while (running) {
synchronized (synchObj) {
try {
showChar(c);
synchObj.wait(interv * 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public synchronized static void showChar(char c) {
System.out.println(c);
}
public void shutdown() {
running = false;
synchronized (synchObj) {
synchObj.notify();
}
}
public static void main(String[] args) throws InterruptedException {
Wat w1 = new Wat('A', 3);
Wat w2 = new Wat('B', 4);
Wat w3 = new Wat('C', 5);
w1.start();
w2.start();
w3.start();
Object[] options = { "Shutdown A", "Shutdown B", "Shutdown C" };
int option;
while (w1.isAlive() || w2.isAlive() || w3.isAlive()) {
option = JOptionPane.showOptionDialog(null,
"Which one would you like to shut?", "Threads",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
switch (option) {
case JOptionPane.YES_OPTION:
w1.shutdown();
break;
case JOptionPane.NO_OPTION:
w2.shutdown();
break;
case JOptionPane.CANCEL_OPTION:
w3.shutdown();
break;
}
Thread.sleep(1);
}
}
}
Your code will probably work fine, but the Thread.sleep is not very elegant. I would do something along these lines, calling the shutdown() method to quit the thread
Object synchObj = new Object();
public void run() {
while (running) {
synchronized (synchObj) {
try {
System.out.println(new Date());
synchObj.wait(5000);
} catch (InterruptedException e) {
// error handling
}
}
}
}
public void shutdown() {
running = false;
synchronized (synchObj) {
synchObj.notify();
}
}
public static void main(String[] args) throws InterruptedException,
IOException {
ThreadTest test = new ThreadTest();
test.start();
BufferedReader tReader = new BufferedReader(new InputStreamReader(
System.in));
tReader.readLine();
test.shutdown();
}
EDIT added test code
Yes, you are closing the threads correctly. The only comment is that you are breaking the encupsulation here because the flag running is accessed directly. I'd recommend you to add method shutdown() that changes this flag to false.
EDIT.
I have just noticed that you are calling sleep() inside the loop. This is indeed bad practice in most cases. You should probably call wait(timeout). In this case your shutdown() method will change the flag to false and then call notify() on same monitor. This will cause your thread to exit immediately.
It seems the program is perfect, uses volatile boolean variable and makes it false.
synchronized need only multi thread access. instead of sleep you can use wait, it has a object access rather than static sleep, also any time you can interrupt the waiting.

Java Threads: Wait Notify mechanism with ArrayList

I tried implementing wait/notify mechanism to modify the ArrayList using two separate threads.
It seems to work fine for first iteration but then for second iteration it waits forever in addToArray() method. I am not able to figure out why is it waiting forever in the method? As per my understanding the other thread (removing an item) should pick up when other thread goes to wait.
Please have a look and point out possible bug if any. I know I can use Vector to have thread-safe operation but that is not what I want.
package threadTest;
import java.util.*;
public class DhagaJava {
public static void main(String...strings){
ArrayModification am = new ArrayModification();
Thread t1 = new Thread(new AddToArray(am));
Thread t2 = new Thread(new RemoveFromArray(am));
t1.start();
t2.start();
}
}
class ArrayModification{
boolean added = false;
ArrayList<Integer> al;
ArrayModification(){
al = new ArrayList<Integer>();
}
public synchronized void addToArrayList(int x) {
if (added == true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.al.add(x);
System.out.println(al);
System.out.println("Added!! :)");
added = true;
notifyAll();
}
public synchronized void removeFromList(){
if( added== false){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(al);
this.al.remove(0);
System.out.println("Removed!! :' ");
added = false;
notifyAll();
}
}
class AddToArray implements Runnable{
ArrayModification ma;
AddToArray(ArrayModification m){
this.ma = m;
}
public void run() {
for (int i = 0; i<10; i++)
ma.addToArrayList(i);
}
}
class RemoveFromArray implements Runnable{
ArrayModification ma;
RemoveFromArray(ArrayModification a){
this.ma = a;
}
public void run(){
ma.removeFromList();
}
}
class RemoveFromArray implements Runnable{
ArrayModification ma;
RemoveFromArray(ArrayModification a){
this.ma = a;
}
public void run(){
//for(int j=11;j<20; j++)
ma.removeFromList();
}
}
Output is:
[0]
Added!! :)
[0]
Removed!! :'
[1]
Added!! :)
Only problem you have is the removeFromList runs only once (because you commended out the for loop). That’s the reason why there is no second remove in the log and that addToArrayList starts waiting forever (waits for someone remove the item from the list).
I tried your code after removing the comment and works fine!
Rather than re-invent the wheel, just use a CopyOnWriteArrayList. It comes with concurrency out of the box.
Your notifyAll is inside the synchronized block. So the other thread may be awaken before he can act. So it may be blocked.
I'm not sure I understood your goal, but this construction could be better :
public void addToArrayList(int x) {
synchonized(this.al) {
if (added == true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.al.add(x);
System.out.println(al);
System.out.println("Added!! :)");
added = true;
}
notifyAll();
}
But this is very complex. Do you have a more general goal ? Maybe a task queue with only one thread could suit you better : it would be faster, lighter, simpler, and as parallelized (that is not at all).

Categories