In below code My application run may thread. But How to I notify certain thread.
My task is print all the thread from Server object msg String when String msg change.
class Server{
static String msg;
synchronized void setMsg(String msg){
this.msg = msg ;
notifyAll();
}
synchronized void proccess(){
while(true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" : "+Server.msg);
}
}
}
Here is my thread class :
class MyThread extends Thread {
Server ser ;
public MyThread(Server ser) {
this.ser = ser ;
this.start();
}
public void run() {
ser.proccess();
}
}
Main Meth() :
class Thread_test {
static String[] name = null ;
public static void main(String[] args) throws InterruptedException {
Server ser = new Server();
for (int i = 0 ; i < 5 ; i++){
MyThread t1 = new MyThread(ser);
t1.setName("Thread "+i);
}
while(true){
Thread.sleep(5000);
ser.sendMsg("Msg : current time is = " + System.currentTimeMillis());
}
}
I change the Server message string once every 5 sec. When change the message I call notifyAll(). This notifyall is wakeup all the waiting thread. But what I want is i.e : I create 10 thread and setName Thread_1 Thread_2 ..etc., Now I want to notify some thread like Thread_1, Thread_4 and Thread_9. I try below func
while(true){
Thread.sleep(5000);
ser.sendMsg("Msg : current time is = " + System.currentTimeMillis());
for ( Thread t : Thread.getAllStackTraces().keySet() ){
if(t.getName().equals("Thread_1") || t.getName().equals("Thread_4") || t.getName().equals("Thread_9")){
t.notify();
}
}
}
I got Exception in thread "main" java.lang.IllegalMonitorStateException
Thank you in advance for any help you can provide.
class Server{
String msg;
void sendMsg(String msg){
this.msg = msg ;
}
public void proccess() throws InterruptedException{
while(true){
synchronized (Thread.currentThread()) {
Thread.currentThread().wait();
}
System.out.println(Thread.currentThread().getName()+" : "+this.msg);
}
}
}
Remove the class object wait() and add Thread.wait() and add
synchronized(t){
t.notify();
}
I don't know it is a correct war or Not. If it wrong provide efficient way.
Related
I implemented producer consumer like this.
But it is throwing error.
I tried to use this method of using lock. Link
class Testclass {
Boolean isFresh = false;
int count = 0;
public synchronized void GET(String threadName){
while(!isFresh){
try {
isFresh.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("GET method was called : " + count + " " + threadName);
isFresh = false;
isFresh.notify();
}
public synchronized void PUT(String threadName){
while(isFresh){
try{
isFresh.wait();
}catch( InterruptedException e){
e.printStackTrace();
}
}
count++;
System.out.println("PUT method was called : " + count + " " + threadName);
isFresh = true;
isFresh.notify();
}
}
class Producer implements Runnable{
Testclass q;
String name;
Producer(Testclass q, String name){
this.q = q;
this.name = name;
}
public void run(){
while(true){
int time = (int)(Math.random() * 10000);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
q.PUT(this.name);
}
}
}
class Consumer implements Runnable{
Testclass q;
String name ;
Consumer(Testclass q,String name){
this.q = q;
this.name = name;
}
public void run(){
while(true){
int time = (int)(Math.random() * 10000);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
q.GET(this.name);
}
}
}
public class Main {
public static void main(String[] args){
Testclass t = new Testclass();
Thread t1 = new Thread(new Consumer(t, "consumer 1"));
Thread t2 = new Thread(new Consumer(t, "consumer 2"));
Thread t3 = new Thread(new Producer(t, "producer 1"));
Thread t4 = new Thread(new Producer(t, "producer 2"));
t1.start();
t2.start();
t3.start();
t4.start();
try{
t1.join();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
this implementation throws following error.
Please explain
why all the threads are throwing Illegal MonitorStateException?
PUT method was called : 1 producer 1
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.PUT(Main.java:34)
at Producer.run(Main.java:54)
at java.base/java.lang.Thread.run(Thread.java:832)
GET method was called : 1 consumer 1
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.GET(Main.java:19)
at Consumer.run(Main.java:76)
at java.base/java.lang.Thread.run(Thread.java:832)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.wait(Native Method)
at java.base/java.lang.Object.wait(Object.java:321)
at Testclass.GET(Main.java:12)
at Consumer.run(Main.java:76)
at java.base/java.lang.Thread.run(Thread.java:832)
PUT method was called : 2 producer 2
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.PUT(Main.java:34)
at Producer.run(Main.java:54)
at java.base/java.lang.Thread.run(Thread.java:832)
Process finished with exit code 0
I want to know why is the output like that?
and what is the correct way of implementing it?
You should use a BlockingQueue. Then you don't have to use wait/notify or even synchronzied.
class Testclass {
BlockingQueue<Integer> queue = new ArrayBlockingQueue(1);
AtomicInteger count = new AtomicInteger(0);
public void GET(String threadName) throws InterruptedException{
Integer i = queue.take();
count.getAndIncrement();
System.out.println("GET method was called : " + count + " " + threadName);
}
public void PUT(String threadName) throws InterruptedException{
int c = count.get();
queue.put(c);
count.getAndIncrement();
System.out.println("PUT method was called : " + count + " " + threadName);
}
}
I'm not sure what you want to do with count. This is readily extensible by changing the size of your Queue.
I have a simple code, consisting of 4 threads (2 modify the data and 2 read the data). I just wrote this sample code to play around with Semaphor and I am not sure why I get ava.base/java.util.ArrayList$Itr.checkForComodification exception? Here are the source code and thanks for any insights.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
public class SemaphoreExample {
public static class InnerWriterSemaphoreThread implements Runnable {
private final List<String> fList;
private final Semaphore fWriteSem;
InnerWriterSemaphoreThread(List<String> list, Semaphore w) {
fList = list;
fWriteSem = w;
}
private void prune() {
System.out.println(Thread.currentThread().getName()+" in prune()..");
for (String s : fList) {
fList.remove(s);
}
}
#Override
public void run() {
String name = Thread.currentThread().getName();
String text;
while (true) {
text = RandomTextGenerator.getRandomSNumbertring();
try {
while(!fWriteSem.tryAcquire()){
System.out.println(name+" waiting to accquire semaphore to write..");
Thread.sleep(0L,4);
}
if (fList.size() > 10) {
prune();
}
fList.add(text);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
fWriteSem.release();// notify readers that write has completed
System.out.println(name+" finished writing, releasing semaphore..");
}
}//while()
}//run()
}//WriterSemaphoreThread
public static class InnerReaderSemaphoreThread implements Runnable {
private final List<String> fList;
private final Semaphore fWriteSem;
InnerReaderSemaphoreThread(List<String> list,Semaphore w) {
fList = list;
fWriteSem = w;
}
private void sleep(){
try{
Thread.sleep(0L, 4);
}catch(InterruptedException e){
e.printStackTrace();
}
}
#Override
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name + " in run()..");
try {
while(fList.isEmpty()){
System.out.println(name+" list is empty, going to sleep..");
sleep();
}
while(!fWriteSem.tryAcquire()){
System.out.println(name+" waiting to accquire semaphor to read..");
Thread.sleep(0l,4);
}
for (String text : fList) {
System.out.println(name + " reading from list " + text);
}
}catch (InterruptedException e) {
e.printStackTrace();
}
finally{
fWriteSem.release(); //Notify threads who want to write to the list
System.out.println(name+" finished reading, releasing semaphore and going to sleep..");
sleep();
}
}
}
}//ReaderSemaphoreThread
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Semaphore r = new Semaphore(1);
Thread th1 = new Thread(new InnerWriterSemaphoreThread(list, r), "Thread 1");
Thread th2 = new Thread(new InnerReaderSemaphoreThread(list, r), "Thread 2");
Thread th3 = new Thread(new InnerWriterSemaphoreThread(list, r), "Thread 3");
Thread th4 = new Thread(new InnerReaderSemaphoreThread(list, r), "Thread 4");
th2.start();
th4.start();
th1.start();
th3.start();
}
}
Above is the sample source code
As #assylias mentioned in comment it happens when you remove elements from list in foreach loop. Just replace
for (String s : fList) {
fList.remove(s);
}
with
fList.clear();
This question already has answers here:
How to use wait and notify in Java without IllegalMonitorStateException?
(12 answers)
Closed 5 years ago.
I am learning multi threading and I am trying to understand how to use wait and notify methods of Object class. I have gone through this link https://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example and have written the following program
Waiter
public class Waiter implements Runnable {
private Message m;
public Waiter(Message m) {
this.m = m;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(t1 + " thread waiting for message");
synchronized (m) {
try {
m.wait();
System.out.println(t1 + " " + m.getText());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(t1 + " thread waiting for message");
}
}
Notifier
public class Notifier implements Runnable {
private Message m;
public Notifier(Message m) {
this.m = m;
}
public void run() {
synchronized (m) {
try {
Thread.sleep(2000);
m.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test
public class WaitNotifyTest {
public static void main(String[] str) {
Message m = new Message("hello");
new Thread(new Waiter(m), "t1").start();
new Thread(new Waiter(m), "t2").start();
new Thread(new Notifier(m)).start();
}
}
When I execute the program, it sometimes terminates properly, sometimes it waits indefinitely, sometimes one of the thread terminates and the other waits indefinitely. Can anyone please tell me what is wrong here?
Also I want to know few examples of real time applications of wait and notify methods.
when you are doing wait best practice is do in a while loop with a condition.There can be scenario where thread will notify and after that other thread enter wait state.So thread will go always in wait state
Modified code:
public class Waiter implements Runnable {
private Message m;
public Waiter(Message m) {
this.m = m;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " thread waiting for message");
synchronized (m) {
try {
while (m.getText() == null) {
m.wait();
}
System.out.println(name + " " + m.getText());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + " thread waiting for message");
}
}
public class Notifier implements Runnable {
private Message m;
public Notifier(Message m) {
this.m = m;
}
public void run() {
synchronized (m) {
m.setText("hello");
m.notifyAll();
}
}
}
public class WaitNotifyTest {
public static void main(String[] str) {
Message m = new Message();
new Thread(new Waiter(m), "t1").start();
new Thread(new Waiter(m), "t2").start();
new Thread(new Notifier(m)).start();
}
}
I am trying to create a program that will carry on running automatically without me having to do anything. I am a bit confused on how to implement runnable in java so I can create a thread that will go to sleep for a certain period of time and then run the re-run the program after the sleep period is over.
public class work {
public static void main(String[] args) throws IOException, InterruptedException {
work test = new work();
test.information();
}
private ConfigurationBuilder OAuthBuilder() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("dy1Vcv3iGYTqFif6m4oYpGBhq");
cb.setOAuthConsumerSecret("wKKJ1XOPZbxX0hywDycDcZf40qxfHvkDXYdINWYXGUH04qU0ha");
cb.setOAuthAccessToken("4850486261-49Eqv5mogjooJr8lm86hB20QRUpxeHq5iIzBLks");
cb.setOAuthAccessTokenSecret("QLeIKTTxJOwpSX4zEasREtGcXcqr0mY8wk5hRZKYrH5pd");
return cb;
}
public void information() throws IOException, InterruptedException {
ConfigurationBuilder cb = OAuthBuilder();
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
try {
User user = twitter.showUser("ec12327");
Query query = new Query("gym fanatic");
query.setCount(100);
query.lang("en");
String rawJSON =null ;
String statusfile = null;
int i=0;
try {
QueryResult result = twitter.search(query);
for(int z = 0;z<5;z++){
for( Status status : result.getTweets()){
System.out.println("#" + status.getUser().getScreenName() + ":" + status.getText());
rawJSON = TwitterObjectFactory.getRawJSON(status);
statusfile = "results" + z +".txt";
storeJSON(rawJSON, statusfile);
i++;
}
}
System.out.println(i);
}
catch(TwitterException e) {
System.out.println("Get timeline: " + e + " Status code: " + e.getStatusCode());
if(e.getErrorCode() == 88){
Thread.sleep(900);
information();
}
}
} catch (TwitterException e) {
if (e.getErrorCode() == 88) {
System.err.println("Rate Limit exceeded!!!!!!");
Thread.sleep(90);
information();
try {
long time = e.getRateLimitStatus().getSecondsUntilReset();
if (time > 0)
Thread.sleep(900000);
information();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
private static void storeJSON(String rawJSON, String fileName) throws IOException {
FileWriter fileWriter = null;
try
{
fileWriter = new FileWriter(fileName, true);
fileWriter.write(rawJSON);
fileWriter.write("\n");
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
} finally {
if(fileWriter!=null) {
fileWriter.close();
}
}
}
}
You have severable options to implement a thread in Java.
Implementing Runnable
When a class implements the Runnable interface, he has to override the run() method. This runnable can be passed to the constructor of a Thread. This thread can then be executed using the start() method. If you'd like to have this thread run forever and sleep, you could do something like the following:
public class HelloRunnable implements Runnable {
public void run() {
while(true){
Thread.sleep(1000);
System.out.println("Hello from a thread!");
}
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
Extending Thread
Thread itself also has a run() method. When extending thread, you can override the Thread's run() method and provide your own implementation. Then you'd have to instantiate your own custom thread, and start it in the same way. Again, like the previous you could do this:
public class HelloThread extends Thread {
public void run() {
while(true){
Thread.sleep(1000);
System.out.println("Hello from a thread!");
}
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
Source: Oracle documentation
Building on the previous answer, you need to either extend Thread or implement Runnable on your Work class. Extending Thread is probably easier.
public class work extends Thread {
public void run() {
// your app will run forever, consider a break mechanism
while(true) {
// sleep for a while, otherwise you'll max your CPU
Thread.sleep( 1000 );
this.information();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
work test = new work();
test.start();
}
// ... rest of your class
}
public static void main(String[] args){
Thread thread = new Thread(runnable); // create new thread instance
thread.start(); // start thread
}
public static Runnable runnable = new Runnable(){
#Override
public void run(){
final int DELAY = 500;
while(true){
try{
// Code goes here;
Thread.sleep(DELAY)
} catch(Exception e){
e.printStackTrace();
}
}
}
}
Please show me how to make thread wait. for example wait if i == 0 and go again when i == 1
public class Main {
public Main() {
}
public void method() {
Thread thread = new Thread(new Task());
// I want to make wait it when I want
// for example wait if i == 0 and go again when i = 1
}
public static void main(String[] args) {
new Main();
}
}
This is suitable for a CountDownLatch.
public static void main( String[] args ) throws Exception {
final CountDownLatch latch = new CountDownLatch( 1 );
System.out.println( "Starting main thread" );
new Thread( new Runnable() {
public void run() {
System.out.println( "Starting second thread" );
System.out.println( "Waiting in second thread" );
try {
latch.await();
} catch ( InterruptedException e ) {
e.printStackTrace();
}
System.out.println( "Stopping second thread" );
}
} ).start();
Thread.sleep( 5000 );
System.out.println( "Countdown in main thread" );
latch.countDown();
Thread.sleep( 1000 );
System.out.println( "Stopping main thread" );
}
You might be able to do this with a semaphore
To avoid active waiting try use wait() and notify() or notifyAll() methods. Wait() can make thread stop until someone call notify() or notifyAll() on same object as wait(). One of condition is that thread must be in possession of monitor of object on which will be invoking wait(), notify() or notifyAll().
Here is an example
import java.util.concurrent.TimeUnit;
public class StartPauseDemo extends Thread {
volatile int i = 1;
public void pause() {
i = 0;
}
public synchronized void unPause() {
i = 1;
notify();// wake up thread
}
#Override
public void run() {
while (i==1) {
// logic of method for example printing time every 200 miliseconds
System.out.println(System.currentTimeMillis());
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i==0) {
synchronized (this) {// thread must possess monitor of object on
// which will be called wait() method,
// in our case current thread object
try {
wait();// wait until someone calls notify() or notifyAll
// on this thred object
// (in our case it is done in unPause() method)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// test - pausing and unpausing every 1 sec
public static void main(String[] args) throws InterruptedException {
StartPauseDemo sp = new StartPauseDemo();
sp.start();// start thread
while (true) {
System.out.println("pausing");
sp.pause();
TimeUnit.SECONDS.sleep(1);
System.out.println("unpausing");
sp.unPause();
TimeUnit.SECONDS.sleep(1);
}
}
}
Output:
pausing
unpausing
1338726153307
1338726153507
1338726153709
1338726153909
1338726154109
pausing
unpausing
1338726155307
1338726155507
... and so on
Using such a flag is not necessarily the best approach, but to answer your specific question: you could make your int volatile. See below a simple example that you can run as is - the fact that i is volatile is crucial for this to work.
The output is (it could be different from run to run due to thread interleaving):
i=1
I'm doing something
I'm doing something
i=0
I'm waiting
I'm waiting
i=1
I'm doing something
I'm doing something
I'm doing something
i=0
I'm waiting
I'm waiting
interrupting
I was interrupted: bye bye
public class TestThread {
private static volatile int i = 0;
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable() {
#Override
public void run() {
try {
while (true) {
while (i == 1) {
System.out.println("I'm doing something");
Thread.sleep(5);
}
while (i == 0) {
System.out.println("I'm waiting");
Thread.sleep(5);
}
}
} catch (InterruptedException ex) {
System.out.println("I was interrupted: bye bye");
return;
}
}
};
Thread t = new Thread(r);
t.start();
i = 1;
System.out.println("i=1");
Thread.sleep(10);
i = 0;
System.out.println("i=0");
Thread.sleep(10);
i = 1;
System.out.println("i=1");
Thread.sleep(10);
i = 0;
System.out.println("i=0");
Thread.sleep(10);
t.interrupt();
System.out.println("interrupting");
}
}