i have an error in this code that's the thread set 2 value instead of one when the program start , the program mustn't set the next value before get the current
this is the main class
class DATA
{
private int value=0;
Lock lock;
Condition co;
Boolean IstReady=false;
public DATA()
{
IstReady=false;
lock = new ReentrantLock();
co=lock.newCondition();
}
public void set(int x) throws InterruptedException
{
lock.lock();// try
while(IstReady==true)
co.await();
value=x;
IstReady=true;
co.signal();
lock.unlock();
}
public int get()
{
int ret=0;
try{
lock.lock();
while(IstReady==false)
co.await();
ret=value;
co.signal();
//lock.unlock();
}
catch(Exception e)
{
System.out.println(e);
}
finally{
lock.unlock();
IstReady=false;
}
return ret;
}
}
}
}
and this the set() and get() class
class setter extends Thread
{
DATA D;
public setter(DATA X)
{
D=X;
}
#Override
public void run()
{
Random T=new Random();
while(true)
{
try {
int M=T.nextInt(1000);
System.out.println("setter set"+M);
D.set(M);
} catch (InterruptedException ex) {
Logger.getLogger(setter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public class Newtest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
DATA x=new DATA();
setter s=new setter(x);
getter g=new getter(x);
s.start();
g.start();
}
}
i think the error is in the set() and get() method in DATA class
Related
I have a scenario in which I thread should monitor Clickhouse and the other one should sqlite. In order to load driver classes, I am using Class.forName("..."). Since the threads are starting at a time. I think it is going under deadlock situation..
Here is the piece of code that describes the issue.
public class ch {
public static void main(String[] args) throws Exception {
new OTTLCleaner().start(); // loading CH Driver
new CHThr().start(); // loading CH Driver
Spark.getInstance(); // loading Sqlite Driver
}
}
class OTTLCleaner extends Thread {
#Override
public void run() {
try {
System.out.println("Inside OTTL");
DBHelper.deleteTables();
...
} catch (Exception e) {
System.out.println(e);
}
}
}
class CHThr extends Thread {
#Override
public void run() {
try {
System.out.println("Inside CHThr");
DBHelper.CHConn();
...
} catch (Exception e) {
System.out.println(e);
}
}
}
class Spark implements Runnable {
private static class SingletonHelper {
private static final Spark INSTANCE = new Spark();
}
public static Spark getInstance() {
return SingletonHelper.INSTANCE;
}
private Spark() {
try {
System.out.println("Inside Spark");
DBHelper.SqliteConn();
...
} catch (Exception e) {
System.out.println(e);
}
}
#Override
public void run() {
...
}
}
class DBHelper {
public static void CHConn() throws ClassNotFoundException, SQLException {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
System.out.println("CH");
...
}
static synchronized void SqliteConn() throws Exception {
Class.forName("org.sqlite.JDBC");
System.out.println("spark");
...
}
public static void deleteTables() throws Exception {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
System.out.println("OTTl");
...
}
}
I added Thread.sleep in between the threads, it works. It is a bad approach in real world. I may get one more thread which may needs to load diff driver in future. What is the good approach to implement this without fail?
I have the following Java code:
But the synchronized not work well, Help!
java.util.ConcurrentModificationException
java.util.ConcurrentModificationException at
java.util.HashMap$HashIterator.nextNode(HashMap.java:1442) at
java.util.HashMap$KeyIterator.next(HashMap.java:1466) at
java.util.AbstractCollection.toArray(AbstractCollection.java:196) at
Main.m(Main.java:68) at Main.lambda$main$0(Main.java:25) at
java.lang.Thread.run(Thread.java:748)
public class Main {
public static Set<Object> objectSet = new HashSet<>();
public static void main(String[] args) throws Exception {
new Thread(()->{m();}).start();
new Thread(()->{add();}).start();
}
public static void add() {
while (true){
objectSet.add(new Object());
}
}
public static void m(){
while(true){
try {
synchronized (objectSet) {
List a = Arrays.asList(objectSet.toArray(new Object[0]));
System.out.println(a.size());
}
}catch (Exception e){
e.printStackTrace();
}
try {
Thread.sleep(1000);
}catch (Exception e){
}
}
}
}
Can't synchronize (java.util.ConcurrentModificationException)
The writers should also synchronize on the same object
Change the add method as
public static void add() {
while (true) {
synchronized (objectSet) {
objectSet.add(new Object());
}
}
}
I have an object A on which I'm updating some data every second and other objects B and C which want to use the data only once per update.
Every object work in parallel.
How can I make B and C wait for the update in A ?
I've seen some similar questions but their responses didn't help me.
I've seen that I could use a "synchronized" bloc on an object D, but they just put the bloc without telling how to instanciate or share that object.
The following code is what I use for my tests. I managed to get them working in parallel but I'm stuck with the suspending part.
This is the class for A
public class Master{
public static void main(String[] args) throws Exception {
Worker B = new Worker("B");
B.start();
Worker C = new Worker("C");
C.start();
while(true)
{
Thread.sleep(1000);
// update data
// notify every thread waiting that they can resume
}
}
}
This is the class used for B and C
public class Worker extends Thread
{
Worker(String name)
{
super("Worker " + name);
}
public void run()
{
int i = 0;
while(!this.isInterrupted())
{
// wait for A to update data
System.out.println(i);
i++;
}
System.out.println("thread interrupted");
}
}
From there, what do I need to add for the purpose I'm looking for ?
To do it very low level, only using the lang APIs, you should use wait/notifyAll.
Not that I used Main.class as an arbitrary object to synchronize
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
Worker w1 = new Worker("Worker 1", sharedData);
Worker w2 = new Worker("Worker 2", sharedData);
w1.start();
w2.start();
while (true) {
try {
Thread.sleep(1000);
sharedData.increase();;
System.out.println("Master: " + sharedData.value());
synchronized (Main.class) {
Main.class.notifyAll();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class SharedData {
private int data = 0;
public void increase () {
data++;
}
public int value() {
return data;
}
}
class Worker extends Thread {
private String workerName;
private SharedData sharedData;
public Worker(String workerName, SharedData sharedData) {
super();
this.workerName = workerName;
this.sharedData = sharedData;
}
#Override
public void run() {
while (true) {
try {
synchronized (Main.class) {
Main.class.wait();
}
System.out.println(workerName + ": " + sharedData.value());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Not sure if I understand you correctly, but this might be worth checking out for you:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
Why use threads at all? Why not just do this?
public class Master {
public static void main(String[] args) {
Worker B = new Worker("B");
Worker C = new Worker("C");
while(true) {
Thread.sleep(1000);
updateData();
B.doWork();
C.doWork();
}
}
}
public class Worker
{
public void doWork() {
System.out.println(i);
i++;
}
private int i = 0;
}
I'm trying to stop a java thread from a different class, but unable to figure out. I have looked into the below links, googled a lot from past 2 days but unable to nail down. May be a simple thing which i need to change but i'm out of options and hence posting it here.
Referred Links
java external threads (outside the class file it's used)
http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html
http://www.java2novice.com/java_thread_examples/
While typing the question, I referred the below links as well..
Stop a thread from outside
Below is my code sample. I'm able to start the WorkerThread from the MainThread and get into the loop. But unable to stop the thread started using the StopThread class.
I've also used the volatile option suggested in the below link.
http://tutorials.jenkov.com/java-concurrency/volatile.html
I feel I'm making a simple mistake, but not able to identify it.
//class WorkerThread
package main;
public class WorkerThread implements Runnable
{
public WorkerThread() {
isRunning = true;
}
public WorkerThread(boolean False) {
isRunning = False;
}
private volatile boolean isRunning;
public synchronized void stopThread() {
isRunning = false;
}
public synchronized boolean IsThreadRunning() {
return isRunning;
}
#Override
public void run()
{
int i = 1;
while(isRunning)
{
System.out.println("Loop " + i);
i++;
try { Thread.sleep(2000); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
//class MainThread
package main;
public class MainThread
{
public static Thread t;
public static void main(String[] args)
{
t = new Thread(new WorkerThread());
t.start();
}
}
//class StopThread
package main;
public class StopThread
{
public static void main(String[] args)
{
//What should i write here to stop the thread started by MainThread
MainThread.t.interrupt();
}
}
public class MainThread
{
public static Thread t;
public static void main(String[] args)
{
t = new Thread(new WorkerThread());
t.start();
}
}
public class StopThread
{
public static void main(String[] args)
{
MainThread.t.interrupt();
}
}
It is not safe to call Thread.stop() it is listed as deprecated in JavaDocs
Also this may be just for the sake of this question, but why does your program have two main methods?
You have an opportunity to make use of what you defined volatile variable and gracefully come out of thread like below:
public class MainThread
{
public static WorkerThread workerThread;
public static void main(String[] args)
{
workerThread = new WorkerThread();
Thread t = new Thread(workerThread);
t.start();
}
}
public class StopThread
{
public static void main(String[] args)
{
Main.workerThread.stopThread();
}
}
Note: This solution works but not a perfect solution.
You can write and read value of isRunning variable from a properties file. This way you can have interaction between two different java processes. ThreadWorker just creates file upon initiation & and just makes attempt to read the file after that. StopThread modifies the properties file when triggered which should be picked up by ThreadWorker.
Check below example:
public class ThreadWorker implements Runnable
{
public volatile static boolean isRunning = false;
public ThreadWorker() {
Properties p = new Properties();
p.setProperty("isRunning", "1");
FileOutputStream out;
try {
//Writes all properties in appProperties file
out = new FileOutputStream("appProperties");
p.store(out, "---Thread Status----");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void run()
{
int i = 1;
String status = "1";
while("1".equals(status))
{
status = getStatus();
System.out.println("Loop " + i);
i++;
try { Thread.sleep(2000); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
public String getStatus() {
FileInputStream in;
Properties p = new Properties();
try {
in = new FileInputStream("appProperties");
p.load(in);
return p.getProperty("isRunning");
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//class StopThread
public class StopThread
{
public static void main(String[] args)
{
Properties p = new Properties();
p.setProperty("isRunning", "0");
FileOutputStream out;
try {
out = new FileOutputStream("appProperties");
p.store(out, "---Thread Status----");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//class StopThread
public class StopThread
{
public static void main(String[] args)
{
Properties p = new Properties();
p.setProperty("isRunning", "0");
FileOutputStream out;
try {
out = new FileOutputStream("appProperties");
p.store(out, "---Thread Status----");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Make thread t a public member of class MainThread, and then just call MainThread.t.interrupt() from StopThread
I have following classes :
package com.akshu.multithreading;
public class ThreadResource {
static int a;
static boolean Value =false;
public synchronized int getA() {
while(Value == false){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Value= false;
notify();
return a;
}
public synchronized void setA(int a) {
while(Value == true)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ThreadResource.a = a;
Value=true;
notify();
}
}
------------------
/**
*
*/
package com.akshu.multithreading;
/**
* #author akshu
*
*/
public class MyThreadA implements Runnable {
int a = 0;
ThreadResource tR= new ThreadResource();
#Override
public void run() {
for (int i = 0; i < 15; i++) {
tR.setA(++a);
System.out.println(" value of a :"+a);
}
}
}
------------
package com.akshu.multithreading;
public class MyThreadB implements Runnable {
#Override
public void run() {
// TODO Auto-generated method stub
ThreadResource tR =new ThreadResource();
for (int i = 0; i < 15; i++) {
System.out.println("getA()"+tR.getA());
}
}
}
----
package com.akshu.multithreading;
public class ThreadExecutionPoint {
public static void main(String args[]) {
Thread th1 = new Thread(new MyThreadA());
Thread th2 = new Thread(new MyThreadB());
th1.start();
th2.start();
}
}
I am trying to understand producer consumer problem via above code .When i execute the above code i am getting
value of a :1
getA()1
Program gets stuck here only (do not gets terminate).
Someone Please explain what wrong i am doing here?
Declare Value as volatile
I.e. static volatile boolean Value =false;
You have declared your set/get methods synchronized. This means that they are lock on this (the object's intrinsic lock).
But in your code you instantiate a different ThreadResource for each thread thereby not making them synchronized since this is different for each case.
Change your code as follows:
public class MyThreadA implements Runnable {
ThreadResource tR;
public MyThreadA(ThreadResource tr) {
this.tR = tr;
}
// your run method here NOT declaring a ThreadResource anymore!!!
}
and same for MyThreadB
Then in ThreadExecutionPoint
ThreadResource tr = new ThreadResource();
Thread th1 = new Thread(new MyThreadA(tr));
Thread th2 = new Thread(new MyThreadB(tr));