What is wrong with the synchronization in following code - java

SYNCHRONIZATION
I have declared a class b which has a synchronized method which is accessed in class c:
class b {
String msg;
public synchronized void foo() {
System.out.print("[" + msg);
try {
Thread.sleep(1000); // Threads go to sleeep
} catch (InterruptedException e) {
System.out.println("Caught" + e);
}
System.out.println("]");
}
}
class a implements Runnable {
b ob;
Thread t;
a(String msg, b obb) {
ob = obb;
ob.msg = msg;
t = new Thread(this); // creating a thread
t.start();
}
public void run() {
ob.foo(); // calling method of class b
}
public static void main(String... a) {
b obb = new b();
a ob = new a("Hello", obb); /* PASSING */
a ob1 = new a("Synch", obb); /* THE */
a ob2 = new a("World", obb);/* MESSAGE */
try {
ob.t.join();
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Caught" + e);
}
}
}
I am expecting the output:
[Hello]
[Synch]
[World]
But the code gives:
[World]
[World]
[World]
Help me with some suggestions. I am a naive JAVA user.

use the following code to get the expected answer.
class b {
// String msg;
public void foo(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000); // Threads go to sleeep
} catch (InterruptedException e) {
System.out.println("Caught" + e);
}
System.out.println("]");
}
}
public class Threading implements Runnable {
b ob;
String msg;
Thread t;
Threading(String message, b obb) {
ob = obb;
msg = message;
t = new Thread(this); // creating a thread
t.start();
}
public void run() {
synchronized (ob) {
ob.foo(msg); // calling method of class b
}
}
public static void main(String... a) {
b obb = new b();
Threading ob = new Threading("Hello", obb); /* PASSING */
Threading ob2 = new Threading("World", obb); /* THE */
Threading ob1 = new Threading("Synch", obb);/* MESSAGE */
try {
ob.t.join();
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Caught" + e);
}
}
}

In the code you have set ob.msg=msg; msg gets overridden by the threads. So you have the same message for all the Threads.
In the constructor of each Thread of class a you are passing the same object of class b. Now all three threads are setting the value of msg of the instance of class b. So one value overrides the other. What you have is the value set by last thread i.e. World.
IMO: Save the msg as an instance variable in each thread and pass it to the foo method as a parameter.
And please follow Java naming convention i.e. Camel Casing

First of all reformat your code. It is very hard to read.
Second when you call ob.msg = msg; it overrites msg value in ob and as it is not synchronized so you cannot actually predict what the output will be.

Related

Are constructors and/or initializations thread safe?

Imagine the following class.
class Whatever {
Object obj;
Whatever() {
obj = new Object();
}
Object get() {
return obj;
}
}
As threads usually write to their local cache I assume the following:
If a thread A creates an instance of Whatever and a thread B calls get() on that instance, it could happen that B gets a null because the reference for obj was written in the locale cache of A only, not in the shared cache.
Am I right? And what about other kind of initializations?
I've tried to produce that behaviour with the following code.
class Main {
static Whatever whatever = new Whatever();
static class Whatever {
Object obj;
Whatever() {
obj = new Object();
}
}
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (!Thread.interrupted()) {
whatever = new Whatever();
}
});
Thread t2 = new Thread(() -> {
while (!Thread.interrupted()) {
System.out.println(whatever.obj);
}
});
t1.start();
t2.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
t2.interrupt();
}
}
The output is never null but I think it should be sometimes. Maybe the code is just trying it wrongly ..

Why am I not getting synchronized access in my code?

class Caller extends Thread
{
String s;
Caller(String s)
{
this.s=s;
}
void call(String msg)
{
synchronized (this)
{
System.out.print("["+msg);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("]");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public void run()
{
call(s);
}
}
public class SynchronisedBlock {
public static void main(String[] args) {
Caller c=new Caller("hi");
Caller c1=new Caller("li");
Caller c2=new Caller("wi");
c.start();
c1.start();
c2.start();
}
}
public class SynchronisedBlock {
public static void main(String[] args) {
Caller c=new Caller("hi");
Caller c1=new Caller("li");
Caller c2=new Caller("wi");
c.start();
c1.start();
c2.start();
}
}
I am trying to get synchronized access to call() using synchronized block but not getting that feature.When I am putting call() method in another class then I am getting the required functionality but not in this code. What am I missing? Can anyone tell me why? Thanx in advance.
As #svasa has stated, you need to have a common synchronizing object.
The simplest working example for your code would be to use a shared lock object. Also, the lock is final to prevent it from being replaced somewhere else in the code, you would end up synchronizing on different objects. (thanks to #P.J.Meisch):
class Caller extends Thread {
String s;
private static final Object lock = new Object();
Caller(String s) {
this.s = s;
}
void call(String msg) {
synchronized (lock) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("]");
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
}
}
public void run() {
call(s);
}
If your run method were in a Runnable like it's supposed to be, instead of a Thread, which it's not, then you could pass the same Runnable instance to each thread and this would work as a synchronization object reference.

Why threads are working in this order?

I have 3 classes: Main, Caller and CallMe. Caller implements Runnable. I pass to Caller constuctor a pointer to instance of CallMe and a message that Caller pass to method of instance of CallMe which print the message. Next, I start a thread.
In Main I create 3 instances of Caller and I except that here it will be printed:
[Welcome]
[to synchronized]
[world!]
But I get the following result:
[Welcome]
[world!]
[to synchronized]
Why?
There is the code:
Main.java
public class Main {
public static void main(String[] args) {
CallMe target = new CallMe();
Caller ob1 = new Caller(target, "Welcome");
Caller ob2 = new Caller(target, "to synchronized");
Caller ob3 = new Caller(target, "world!");
try {
ob1.thread.join();
ob2.thread.join();
ob3.thread.join();
} catch (InterruptedException exc) {
System.out.println(exc);
}
}
}
Caller.java
public class Caller implements Runnable {
String message;
CallMe target;
Thread thread;
public Caller(CallMe target, String message) {
this.target = target;
this.message = message;
this.thread = new Thread(this);
this.thread.start();
}
public void run() {
synchronized (target) {
target.call(message);
}
}
}
CallMe.java
public class CallMe {
public void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException exc) {
System.out.println(exc);
}
System.out.println("]");
}
}
The three instantiations of Caller in your main method probably all happen within one time slice, i.e. without yielding to another thread. This results in all three threads being created and placed into the runnable state. After that, threads can be scheduled in any order, so you just got unlucky.
If you want threads to execute in a specific order you need to provide the interlocking logic yourself.

getting thread names in stop() method

how can I get in stop() thread names like i did in start()? Thread names are A,B,C,D. My program runs thread in order and stops them in revers order. But I have problem with printing their names. In start() I do it without any problems but in stop() I just dont know how to do it. I'm pretty new in java and this is one of my firs programs that I did that is why i dont know how to do this.
Thank you so much for your help.
Here is the code:
import java.util.*;
class Service extends Thread
{
private RobotController controller;
public String robotID;
private byte[] lock;
public Service(RobotController cntrl, String id)
{
controller = cntrl;
robotID = id;
}
public byte[] getLock() { return lock;}
public void run()
{
lock = new byte[0];
synchronized(lock)
{
byte[] data;
while ((data = controller.getData()) == null)
{
try {
lock.wait();
} catch (InterruptedException ie) {}
}
System.out.println("Thread " + robotID + " Working" );
}
}
}
class RobotController
{
private byte[] robotData;
private Vector threadList = new Vector();
private Service thread_A;
private Service thread_B;
private Service thread_C;
private Service thread_D;
public void setup(){
thread_A = new Service(this, "A");
thread_B = new Service(this, "B");
thread_C = new Service(this, "C");
thread_D = new Service(this, "D");
threadList.addElement(thread_A);
threadList.addElement(thread_B);
threadList.addElement(thread_C);
threadList.addElement(thread_D);
thread_A.start();
thread_B.start();
thread_C.start();
thread_D.start();
start();
stop();
}
public void start()
{
System.out.println("START:");
{
for (int i=0; i <threadList.size(); i++)
{
try {
Thread.sleep(500);
}catch (InterruptedException ie){
System.out.println(ie);
}
putData(new byte[10]);
Service rbot = (Service)threadList.elementAt(i);
byte[] robotLock = rbot.getLock();
synchronized(robotLock) {
robotLock.notify();
}
}
}
}
public void stop()
{
Collections.reverse(threadList);
System.out.println("STOP:");
for ( Object o : threadList) {
System.out.println("Thread "+ o +" Stop");
}
}
public synchronized byte[] getData()
{
if (robotData != null)
{
byte[] d = new byte[robotData.length];
System.arraycopy(robotData, 0, d, 0, robotData.length);
robotData = null;
return d;
}
return null;
}
public void putData(byte[] d) { robotData = d;}
public static void main(String args[])
{
RobotController controller = new RobotController();
controller.setup();
}
}
Thread has name and getter getName(), so if you have instance of thread you can always call thread.getName().
I do not know how do you access the thread name "in start" because I do not see where do you call getName(). However I think I know what's your problem in stop.
You store your threads in Vector. Then you iterate over vector's elements and print thread, so it invokes thread's toString(). You probably have to cast Object to Thread and call its getName():
System.out.println("STOP:");
for ( Object o : threadList) {
System.out.println("Thread "+ ((Thread)o).getName() +" Stop");
}
But once you are done, I'd recommend you to find a good and new enough tutorial on java.
You are using not commonly applicable coding formatting.
You are using Vector instead of List and its implementations.
You are trying to use unclear technique for thread synchronization and management.
Start learning step-by-step. And do not hesitate to ask questions. Good luck.

java thread interleaving

I'm trying to interleave the execution of two independent threads. such that both have a run method with 10 iterations and after every iteration i want to context switch the threads.
thread A starts and after doing something like printing passes control to thread B. then thread B prints and passes control back to A and so on until both finish.
What is the effective mechanism to do this?
I'm attaching a sample code. hope you can help.
// Suspending and resuming a thread for Java 2
class NewThread implements Runnable {
String name; // name of thread
Thread t;
// boolean suspendFlag;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
// suspendFlag = false;
t.start(); // Start the thread
}
public String getState()
{
Thread t=Thread.currentThread();
return t.getState().toString();
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(200);
synchronized(this) {
//SuspendResume.suspendFlag2=false;
SuspendResume.suspendFlag1=true;
while(SuspendResume.suspendFlag1) {
wait();
//System.out.println(SuspendResume.ob1.t.getState().toString());
// if(SuspendResume.ob2.t.getState().toString()=="WAITING")
// SuspendResume.ob2.t.notify();
}
}
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
void mysuspend() {
// suspendFlag = true;
}
synchronized void myresume() {
// suspendFlag = false;
notify();
}
}
class NewThread2 implements Runnable {
String name; // name of thread
Thread t;
// boolean suspendFlag;
NewThread2(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
// suspendFlag = false;
t.start(); // Start the thread
}
public String getState()
{
Thread t=Thread.currentThread();
return t.getState().toString();
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
synchronized(this) {
//SuspendResume.suspendFlag1=false;
//while(SuspendResume.suspendFlag1) {
// while(suspendFlag) {
//wait();
//System.out.println(SuspendResume.ob2.t.getState().toString());
//if(SuspendResume.ob1.t.getState().toString()=="WAITING")
//SuspendResume.ob1.t.notify();
//}
SuspendResume.suspendFlag1=false;
notify();
}
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
void mysuspend() {
// suspendFlag = true;
}
synchronized void myresume() {
// suspendFlag = false;
notify();
}
}
class SuspendResume {
static boolean suspendFlag1=false;
static NewThread ob1 = new NewThread("One");
static NewThread2 ob2 = new NewThread2("Two");
// static boolean suspendFlag2=false;
public static void main(String args[]) {
try {
//Thread.sleep(1000);
//ob1.mysuspend();
//System.out.println("Suspending thread One");
//Thread.sleep(1000);
//ob1.myresume();
//System.out.println("Resuming thread One");
// ob2.mysuspend();
//System.out.println("Suspending thread Two");
Thread.sleep(1000);
// ob2.myresume();
//System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
System.out.println(ob1.getState());
System.out.println(ob1.getState());
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
First off, I'm not sure what kind of scenario you have where you want to run two threads sequentially over and over again. That sounds like a single thread running two different methods in a loop. None-the-less, it sounds like an interesting challenge so I took it up.
Making use of Java 5's Exchanger class, the solution gets pretty small. I ended up with a single Runnable class. I use two instances of them to pass around a boolean true and boolean false to each other. The Exchanger class facilitates the passing around of the boolean values in a thread safe manner. A Runnable only 'executes' its code when it has the boolean true value.
package interleavedexample;
import java.util.concurrent.Exchanger;
import java.util.logging.Logger;
/**
*
*/
public class InterleavedRunnable implements Runnable {
private final String name;
private final Exchanger<Boolean> exchanger;
private Boolean state;
public InterleavedRunnable(String name, Exchanger<Boolean> exchanger,
Boolean state) {
this.name = name;
this.exchanger = exchanger;
this.state = state;
}
#Override
public void run() {
try {
while (true) {
if (state) {
Logger.getLogger(getClass().getName()).info(name + " is running");
}
state = exchanger.exchange(state);
}
} catch (InterruptedException ex) {
Logger.getLogger(name).info("Interrupted");
}
}
Setting up the runnables are quite easy:
public static void main(String[] args) {
Exchanger<Boolean> exchanger = new Exchanger<Boolean>();
Thread thread1 = new Thread(new InterleavedRunnable("Thread 1", exchanger, true));
Thread thread2 = new Thread(new InterleavedRunnable("Thread 2", exchanger, false));
thread1.start();
thread2.start();
}
Anytime you can find existing functionality within the Java API (or well known libraries), you should utilize them to the fullest extent. The less lines of code you write the less lines there are to maintain.
The 'OS Sycnro 101' solution is to use two semaphores, one for each thread, and swap over one 'GO' token/unit between them. Start both threads and then give the token to whichever thread you want to go first.
Use wait and notify for this.
public class Thread1 implements Runnable {
#Override
public void run() {
while(true){
synchronized (Main.obj) {
try {
Main.obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("1");
synchronized (Main.obj) {
Main.obj.notify();
}
}
}
}
public class Thread2 implements Runnable{
#Override
public void run() {
while(true){
synchronized (Main.obj) {
try {
Main.obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("2");
synchronized (Main.obj) {
Main.obj.notify();
}
}
}
}
public class Main {
public volatile static Object obj = new Object();
/**
* #param args
*/
public static void main(String[] args) {
Thread t1 = new Thread(new Thread1());
Thread t2 = new Thread(new Thread2());
t1.start();
t2.start();
synchronized (obj) {
obj.notifyAll();
}
}
}
Did not get your question correctly. If you want to execute thread B only after thread A execution is over, then there is no point of multi-threading at all. You can simply put the thread B contents in thread A run() method.
Still if it is very much required then you can try using wait() and notify() methods on two objects . Something like this.
Class ClassA implements runnable{
Message messageA;
Message messageB;
public ClassA(Message messageA,,Message messageB){
this.messageA = messageA;
this.messageB = messageB;
}
public void run(){
for(;loop contition;){
//code here
messageB.notify();
messageA.wait();
}
}
}
Class ClassB implements runnable{
Message messageA;
Message messageB;
public ClassB(Message messageA,Message messageB){
this.messageA = messageA;
this.messageB = messageB;
}
public void run(){
for(;loop condition;){
messageB.wait();
//code here
messageA.notify();
}
}
}
now create two objects in main messageA and messageB and pass both of them in the constructor of each thread.

Categories