Can I use a static boolean as a lock for a synchronized thread?

I tried using a static boolean to lock and unlock two synchronized threads. So I wrote the following code:

public class Main {

    public static void main(String[] args){

        //MyObject lock = new MyObject();
        Thread1 t1 = new Thread1(100,'#');
        Thread1 t2 = new Thread1(100,'*');

        t1.start();
        t2.start();
    }
}

public class Thread1 extends Thread {

    public static boolean lock;
    int myNum;
    char myChar;

    public Thread1(int num, char c){
        myNum = num;
        myChar = c;
        lock = false;
    }

    public synchronized void run(){
        System.out.println(getName() + " is runing");
        while (Thread1.lock == true){
            System.out.println(getName() + " is waiting");
            try{wait();}
            catch(InterruptedException e){}
        }
        Thread1.lock = true;
        for(int i = 0; i<myNum; i++){
            if(i%10==0)
                System.out.println("");
            System.out.print(myChar);
        }
        Thread1.lock = false;
        notifyAll();
    }
}

I probably am not doing it right, because only one thread prints "mychar" and the other thread just goes into wait (), rather than waking up when I do notifyAll (). I thought this could be a good way to use a static boolean for the whole class, and then change it every time and call notifyAll () to check this flag in other objects ...

Output example

:

Thread-0 is runing

Thread-1 is runing
Thread-1 is waiting
##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
+4
source share
3

notify() wait() "" , . this, Thread1, .

, Thread-0 :

  • lock
  • false
  • notifyAll() this ( , Thread-0).

Thread-1 :

  • lock
  • , true
  • wait() this ( , Thread-1)

Thread-0 notifyAll() this ( ), Thread-1 wait() this ( ), Thread-1 , Thread-0 , .

, , :

public class Thread1 extends Thread {

    private static final Object lock = new Object();

    public void run(){
        // non-sequential code
        System.out.println(getName() + " is running");
        synchronized (lock) {
            // this code will be run sequentially by one thread at a time
        }
        // non-sequential code
    }
}
+4

, , . :

  • , .
  • . , , , - , while, boolean true.
  • notifyAll() , . , .

Java - , concurrency ( ). , :

  • AtomicBoolean ( - , )
  • compareAndSet /, .
  • Wait() AtomicBoolean, , ( notifyAll() ).
+1

.

class Thread1 extends Thread {

    private static final Object lock = new Object();
    int myNum;
    char myChar;

    public Thread1(int num, char c){
        myNum = num;
        myChar = c;
    }

    public void run(){
        System.out.println(getName() + " is runing");
        synchronized(lock) {
            for(int i = 0; i<myNum; i++){
                if(i%10==0)
                    System.out.println("");
                System.out.print(myChar);
            }
        }
    }
}
0

Source: https://habr.com/ru/post/1546664/


All Articles