question archive In Java Programming , A multi-threading application wants to share a common object among multiple thread
Subject:Computer SciencePrice:3.87 Bought7
In Java Programming , A multi-threading application wants to share a common object among multiple thread.
Define the possible solution that can provide an efficient implementation to access sharable object.
Is it possible to share a common variable that can control the execution of multiple threads?
Justify your answer with suitable examples.
Answer:
Shared Object in Multi-threading:
A single object can share between multiple or different threads. Here, there is no possible way to save the state because other threads were also aware of the current state.
For example:
//create class public class TestThread { //main method public static void main(String[] args) { //Initialize an object for the class SharedObj SharedObj so = new SharedObj(); //initialize Thread1 and call the start() method new Thread(so, "Thread 1").start(); //Initialize Thread2 with the same object so new Thread(so, "Thread 2").start(); } } //create class SharedObj which implements the Runnable interface class SharedObj implements Runnable { //initialize the variable VALUE private static final int VALUE = 10; //Initialize ThreadLocal private static final ThreadLocal<Integer> TL = new ThreadLocal<Integer>() { //override the method initialValue() @Override protected Integer initialValue() { return 0; } }; //override the method run() @Override public void run() { //for loop iterate till VALUE=9 for (int i = 0; i < VALUE; i++) { //synchronized the threads synchronized (TL) { //get the value from the ThreadLocal int value = TL.get(); //print the current thread name and the value System.out.println(Thread.currentThread().getName() + ": " + value); //set the value for ThreadLocal TL.set(value + 1); //try block to throw exception try { //notify all the threads TL.notifyAll(); //if the value of variable VALUE-1 is less then i then move the current thread to waiting state if (i < VALUE - 1) { TL.wait(); } } //catch the exception catch (InterruptedException ex) { } } } } }
In the above code, the object so for the class SharedObj is shared between Thread1 and Thread2.
Shared Variable in Multi-threading:
Like an object, a variable can also be shared by different threads.
For example:
//create class public class TestThread { //initialize the variable a static int a = 0; //main method public static void main(String[] args) { //Initialize the class TestThread TestThread tt = new TestThread(); //initialize the inner class Class1 by using the object tt Class1 c1 = tt.new Class1(); //initialize the inner class Class2 by using the object tt Class2 c2 = tt.new Class2(); //initialize the object o1 in class Class1 by using the class Lock object lock1 c1.o1 = Lock.lock1; //initialize the object o2 in class Class1 by using the class Lock object lock2 c1.o2 = Lock.lock2; //initialize the object o1 in class Class2 by using the class Lock object lock2 c2.o1 = Lock.lock2; //initialize the object o2 in class Class2 by using the class Lock object lock1 c2.o2 = Lock.lock1; //Initialize the thread t1 and t2 using c1 and c2 Thread t1 = new Thread(c1); Thread t2 = new Thread(c2); //start the thread t1 and t2 t1.start(); t2.start(); } //static class static class Lock { //initialize two objects lock1 and lock2 final static Object lock1 = new Object(); final static Object lock2 = new Object(); } //inner class implements the runnable interface class Class1 implements Runnable { //initialize object with o1,o2 Object o1; Object o2; //run method public void run() { //while loop iterates till a value reaches 9 while (TestThread.a < 10) { //try block try { //print a value and the class name System.out.println(++TestThread.a + " Class1 "); synchronized (o1) { //notify object o1 o1.notify(); } synchronized (o2) { //o2 to waiting state o2.wait(); } } //catch block catch (InterruptedException ie) { ie.printStackTrace(); } } } } //inner class Class2 class Class2 implements Runnable { //initialize objects o1 and o2 Object o1; Object o2; //method run() public void run() { //iterates till a value becomes 9 while (TestThread.a < 10) { //try block try { synchronized (o2) { //o2 to waiting state o2.wait(); //print a value and the class name System.out.println(++TestThread.a + " Class2 "); } synchronized (o1) { //notify object o1 o1.notify(); } } //catch block catch (InterruptedException ie) { ie.printStackTrace(); } } } } }
In the above example, the variable a is incremented by the thread t1 and thread t2.
Step-by-step explanation
Shared Object in Multithreading:
Snip of the Output:
PFA