I just want to start and stop the thread when return key is pressed.
Here thread is stopped fine but i cant Start that thread again please help.
Also explain me the use of volatile keyword .Is it helpful for me to over come this problem.
public class Sync extends Thread{
public boolean meth= true;
public void run(){
while(meth){
System.out.println("hello");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public void shutdown(){
meth=false;
}
public void startup(){
meth=true;
}
}
MAIN CLASS``
package com.Sync;
import java.util.Scanner;
public class SyncMain {
public static void main(String[] arg) throws InterruptedException{
Sync hi= new Sync();
hi.start();
System.out.println("press enter to stop");
Scanner d= new Scanner(System.in);
d.nextLine();
hi.shutdown();
System.out.println("press enter to start");
d.nextLine();
hi.startup();
}
}
OUTPUT
run:
press enter to stop
hello
hello
hello
hello
press enter to start
BUILD SUCCESSFUL (total time: 6 seconds)
Thread's are not re-entrant, that is, once they exit the run method, they can't be restarted, you need to create a new instance.
One solution would be to create a new instance of Sync and start it, but a better solution, based on you code might, be to use a wait lock to "pause" the thread and the allow it to resume, for example...
public static class Sync implements Runnable {
private AtomicBoolean keepRunning = new AtomicBoolean(true);
private AtomicBoolean pause = new AtomicBoolean(false);
private ReentrantLock lckPause = new ReentrantLock();
private Condition conPause = lckPause.newCondition();
public void run() {
while (keepRunning.get() && !Thread.currentThread().isInterrupted()) {
while (pause.get() && !Thread.currentThread().isInterrupted()) {
lckPause.lock();
try {
System.out.println("Paused");
conPause.await();
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lckPause.unlock();
}
}
if (!Thread.currentThread().isInterrupted()) {
System.out.println("hello");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}
public void setPaused(boolean paused) {
if (pause.get() != paused) {
pause.set(paused);
if (!paused) {
lckPause.lock();
try {
conPause.signal();
} finally {
lckPause.unlock();
}
}
}
}
public void terminate() {
keepRunning.set(false);
setPaused(false);
}
}
This basically sets up two loops, one to keep the thread running until it's "terminated" and one to trap the "pause" condition...
Then you could do something like...
public static void main(String[] args) {
Sync hi = new Sync();
Thread t = new Thread(hi);
t.start();
Scanner d = new Scanner(System.in);
System.out.println("press enter to pause");
d.nextLine();
hi.setPaused(true);
System.out.println("press enter to resume");
d.nextLine();
hi.setPaused(false);
System.out.println("press enter to terminate");
d.nextLine();
hi.terminate();
try {
t.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("Has terminated");
}
to simply run it...
You should note that it's generally discouraged to extend directly from Thread and it is generally encouraged to use a separate Runnable, there are many reasons, but one which you will find most useful in future is Runnable is more widely supported in different parts of the API (like the Executors API) making it a much more flexible option
Have a look at the Concurrency Trail for more details, in particular Lock Objects
Related
I was asked at an interview to write java code which is guaranteed deadlock. I wrote a standard code which presents at every Java book, like create 2 threads and call synchronized methods at different order, sleep a little before call the 2nd.
Of course this stuff didn't satisfy the interviewers, so now I'm proceeding to figure the solution out.
I discovered a piece of code:
public class Lock implements Runnable {
static {
System.out.println("Getting ready to greet the world");
try {
Thread t = new Thread(new Lock());
t.start();
t.join();
} catch (InterruptedException ex) {
System.out.println("won't see me");
}
}
public static void main(String[] args) {
System.out.println("Hello World!");
}
public void run() {
try {
Thread t = new Thread(new Lock());
t.start();
t.join();
} catch (InterruptedException ex) {
System.out.println("won't see me");
}
}
}
But I'm not sure if this code satisfied them? Sure. The code never ends execution, but is it a true deadlock? Aren't deadlocks about synchronization? And, for example, I can also write an endless cycle, put a Thread.sleep inside and name it a "deadlock".
So the question is: is it possible to write a classic deadlock using synchronized methods but 100% guaranteed? (Please don't tell me about very, very, very likely deadlock cases. I know it.)
Thanks.
Create two resources, and have each thread try to get one before releasing the other, but in different orders. For instance:
CountDownLatch a = new CountDownLatch (1);
CountDownLatch b = new CountDownLatch (1);
void one() throws InterruptedException {
a.await();
b.countDown();
}
void two() throws InterruptedException {
b.await();
a.countDown();
}
The thread that runs one can't release b, because it's waiting for a. It'll wait forever, because the thread that runs two can't release a because it's waiting for b.
One or the classic deadlock scenarios is when you acquire locks in reverse order.
class Resource1 {
synchronized static void method1() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
Resource2.method1();
}
}
class Resource2 {
synchronized static void method1() {
Resource1.method1();
}
}
public class MultiThreadApp {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
Resource2.method1();
}
}).start();
Resource1.method1();
}
}
public class Deadlock {
public static void main(String[] args) {
String res1 = "a";
String res2 = "s";
new Thread(
() -> {
synchronized (res1) {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
}
synchronized (res2) {
}
}
}
).start();
new Thread(
() -> {
synchronized (res2) {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
}
synchronized (res1) {
}
}
}
).start();
}
}
This is a part of my program. I am convinced that this class is where my code is getting hung up. I am using deadlock thread in java and I have difficulties.
So I did not want to display my entire code but this is a sample. Can someone tell me why it is hanging? this deadlock situation is confusing me.
public class gameEnemyRelease {
private static Object enemy1 = new Object();
public static Object enemy2= new Object();
public static void main(String args[]) {
player1 p1 = new player1();
player2 p2 = new player2();
p1.start();
p2.start();
}
private static class player1 extends Thread {
public void run() {
synchronized (enemy1) {
System.out.println("Start Launch");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Waiting for enemy 2 to launch...");
synchronized (enemy2) {
System.out.println("Shot");
}
}
}
}
private static class player2 extends Thread {
public void run() {
synchronized (enemy2) {
System.out.println("...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("");
synchronized (enemy1) {
System.out.println("");
}
}
}
}
}
Your program is hanging probably because neither of the threads in position to proceed and waiting for each other to release the lock. So you can try changing this method; Try the below code:
private static class player2 extends Thread {
public void run() {
synchronized (enemy1) {
System.out.println("...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("");
synchronized (enemy2) {
System.out.println("");
}
}
Think about this code path:
Thread 1 locks Enemy 1
Thread 2 locks Enemy 2
Threads 1 try to lock Enemy 2 but is locked by thread 2, so it waits
Thred 2 try to lock Enemy 1 but is locked by thread 1, so it waits
You need to address and probably lock both enemies from start.
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();
}
}
Hi guys, I'm new to Java and I'm takin a course at Uni. I have gotten a task to write a small game that generates a random nr that the player will guess. After guessing the player is supposed to get the option to play again or WAIT and automatically come back to the main menu.
First I tried thread.sleep(5000) but it got stuck because it was waiting for user input (nextLine). Then a friend told be about timer and timertask, which i used and now my game is almost working.
The only problem is when i call a new method from my run() method the old (play again Y/N) thread running in the background is not ended. So when my menu appears after 5 seconds, my first input is connected to the play again Y/N choice and not the main menu options. Here are the kod parts:
public void tryAgain() {
Timer timer = new Timer();
Task timerTask = new Task();
int y = 1;
String yesNo = sc.nextLine();
System.out.println("Try again Y/N");
Statistics.setGames(games);
timer.schedule(timerTask, 5000);
do {
try {
yesNo = sc.nextLine();
if (yesNo.equals("Y") || yesNo.equals("y")) {
guesses = 0;
y = 2;
timerTask.cancel();
playGame();
} else if (yesNo.equals("N") || yesNo.equals("n")) {
y = 3;
timerTask.cancel();
Statistics.setGames(games);
menu.showMainMenu();
} else {
System.out.println("Wrong input, try Y or N:");
}
} catch (Exception e) {
sc.next();
System.out.println("Wrong input, try Y or N:");
}
} while (y == 1);
}
and :
import java.util.TimerTask;
class Task extends TimerTask {
play menu = new play();
public void run() {
Statistics.getGames();
menu.menu.showMainMenu();
cancel();
}
}
You cannot interrupt blocking reads. But you can use the BufferedReader.ready() method, which will tell us whether the underlying stream is ready to be read.
Let's implement a non blocking reader:
public class OurNonBlockingConsoleReader implements Callable<String> {
public String call() throws IOException {
BufferedReader sysInReader = new BufferedReader(new InputStreamReader(System.in));
String answer;
do {
System.out.println("Try again Y/N");
try {
while (!sysInReader.ready()) {
Thread.sleep(100);
}
answer = sysInReader.readLine();
} catch (InterruptedException e) {
return null;
}
} while (!"y".equalsIgnoreCase(answer) && !"n".equalsIgnoreCase(answer));
return answer;
}
}
Next, we call this reader with a timeout, using the ExecutorService and the Future from java.util.concurrent package:
public void tryAgain() throws InterruptedException, ExecutionException {
ExecutorService readerExecutor = Executors.newSingleThreadExecutor();
Future<String> readerResult = readerExecutor.submit(new OurNonBlockingConsoleReader());
try {
String answer = readerResult.get(5, TimeUnit.SECONDS);
if ("y".equalsIgnoreCase(answer)) {
playTheGame();
} else {
goToMainTheMenu();
}
} catch (TimeoutException e) {
goToMainTheMenu();
} finally {
readerExecutor.shutdownNow();
}
}
The readerResult.get(...) call will wait 5 seconds for a valid answer. It there is no valid answer returned from OurNonBlockingConsoleReader.call, the Future.get will raise a TimeoutException.
Addressing only your immediate problem:
Your run method needs to set y to something other than 1 (perhaps 0), then call interrupt on Y/N thread. This will kick it out of a blocking read with an InterruptException or ClosedByInterruptException. (Your catch block will need to be smarter.) Then the Y/N loop will finish because y is not 1. End of problem.
For this to work, y needs to be declared volatile, or each thread might use its own copy. (Accessing it only within synchronized blocks will work also.)
Added Example:
public class YesNo {
private volatile Thread tryAgainThread;
private volatile int y = 1;
public doNotTryAgain() {
y = 0;
tryAgainThread.interrupt();
}
// Called only from tryAgainThread thread.
public void tryAgain() {
do {
try {
// Exactly what you have now.
...
}
catch (Exception e) {}
} while (y == 1);
}
....
class Task extends TimerTask {
public YesNo ynInstance;
...
public void run() {
ynInstance.doNotTryAgain();
Statistics.getGames();
...
}
}
I'll let you figure out how to set tryAgainThread, which is the thread the tryAgain method is called--and is looping--on. Also, Task needs to know the relevant (and probably only) instance of the class that contains the tryAgain call running in the 'tryAgainThread'. In your case some static public fields will do the job, but ideally you'd want something more elegant.
Also, catch (Exception e) {} will work fine, but ideally you'd check your exceptions better.
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