1

I have variables that i want to share between threads but they are giving me some problems! I have an Integer that i want the main program to share with the threads it is creating and allow the threads to change its value and being seen by the other threads and the main program (I hace two different types of threads and I want to create more than one of each). I have already consider the critical section. This is a part of my program:

public static void main(String[] args) throws InterruptedException{
    Integer  t = 0; //What do I change here?
    (new Te(t)).start();
    (new In(t)).start();     
}

I haven't written all my program but in this case the interested variable is t, that is going to be changed by Threads and its new valuable must be seen by the main program in case it creates a new Thread and by the threads when they want to read/write .

If it isnt clear with just these, I can post more code if it is needed!

Thank you very much in advance :)

Marta G.
  • 108
  • 1
  • 9

2 Answers2

6

Integer are immutable. So changes will never be reflected between your threads.
As alternative, you could wrap the Integer in a shared object and provide thread safe methods to access/change the wrapped Integer value.

The client could would look like :

public static void main(String[] args) throws InterruptedException{
    ThreadSafeInteger o = new ThreadSafeInteger();
    new Te(o).start();
    new In(o).start();     
}

Or more simply use AtomicInteger that addresses the requirement as it wraps an int and besides it also ensures atomic update of the int value.

The client could would look like :

public static void main(String[] args) throws InterruptedException{
    AtomicInteger o = new AtomicInteger();
    new Te(o).start();
    new In(o).start();     
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0
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();
            }

        }
    }
}
spandey
  • 1,034
  • 1
  • 15
  • 30