Variable exchange between several different threads

I want to split a variable between multiple threads as follows:

boolean flag = true; T1 main = new T1(); T2 help = new T2(); main.start(); help.start(); 

I would like to share a flag between the main and the reference thread, where these are two different Java classes that I created. Is there any way to do this? Thank you

+8
java multithreading synchronization shared
source share
5 answers

Both T1 and T2 can refer to a class containing this variable. Then you can make this variable mutable, which means that changes to this variable are immediately displayed in both threads.

See this article for more details.

Volatile variables share the visibility functions of a synchronized, but not one of the features of atomicity. This means that threads will automatically see the latest value for variable variables. They can be used to ensure thread safety, but only in a very limited set of cases: those that do not impose restrictions between several variables or between a current variable and its future values.

and look at the pros and cons of using volatile and more complex sharing tools.

+17
source share

In addition to other suggestions, you can also wrap a flag in a control class and make it last in its parent class:

 public class Test { class Control { public volatile boolean flag = false; } final Control control = new Control(); class T1 implements Runnable { @Override public void run() { while ( !control.flag ) { } } } class T2 implements Runnable { @Override public void run() { while ( !control.flag ) { } } } private void test() { T1 main = new T1(); T2 help = new T2(); new Thread(main).start(); new Thread(help).start(); } public static void main(String[] args) throws InterruptedException { try { Test test = new Test(); test.test(); } catch (Exception e) { e.printStackTrace(); } } } 
+6
source share
  • Making it static can solve this problem.
  • Link to the main thread in another thread and display the visible variable
+4
source share

To make it visible between instances of T1 and T2 , you can make two classes containing a reference to an object containing a variable.

If a variable needs to be changed when threads are running, you need to consider synchronization. The best approach depends on your specific requirements, but the main parameters are the following:

  • enter volatile variable;
  • turn it into an AtomicBoolean ;
  • Use full-blown sync around your code.
+3
source share

You can use the lock variables "a" and "b" and synchronize them to lock the "critical section" in the reverse order. For example. Notify β€œa”, then block β€œb”, β€œPRINT”, notify β€œb”, then block β€œa”.

Please read the following code: -

 public class EvenOdd { static int a = 0; public static void main(String[] args) { EvenOdd eo = new EvenOdd(); A aobj = eo.new A(); B bobj = eo.new B(); aobj.a = Lock.lock1; aobj.b = Lock.lock2; bobj.a = Lock.lock2; bobj.b = Lock.lock1; Thread t1 = new Thread(aobj); Thread t2 = new Thread(bobj); t1.start(); t2.start(); } static class Lock { final static Object lock1 = new Object(); final static Object lock2 = new Object(); } class A implements Runnable { Object a; Object b; public void run() { while (EvenOdd.a < 10) { try { System.out.println(++EvenOdd.a + " A "); synchronized (a) { a.notify(); } synchronized (b) { b.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } } } class B implements Runnable { Object a; Object b; public void run() { while (EvenOdd.a < 10) { try { synchronized (b) { b.wait(); System.out.println(++EvenOdd.a + " B "); } synchronized (a) { a.notify(); } } catch (InterruptedException e) { e.printStackTrace(); } } } } 

}

OUTPUT: - 1 A 2 B 3 A 4 B 5 A 6 B 7 A 8 B 9 A 10 B

0
source share

All Articles