同步类

 
package t;


/**
 *
 * @author Vicky.H
 */
public final class Mina2 {

    public static long checkerSleepMillis = 1000;

    public static void reloadResource() throws InterruptedException {
        synchronized (Mina2.class) {
            System.out.println("done 1");
            Thread.sleep(10000);
            System.out.println("done 2");
        }
    }
    
    public static void sayHello() {
        System.out.println("Hello");
    }

    public static void main(String args[]) throws InterruptedException {
        new Mina2Thread().start();
        Mina2.sayHello();
        new Mina2Thread().start();
        
        
        new Mina2Thread2().start();
        Mina2.sayHello();
        new Mina2Thread2().start();
    }
}

class Mina2Thread extends Thread {
    
    @Override
    public void run() {
        try {
            Mina2.reloadResource();
        } catch (InterruptedException ex) {
        }
    }
}

class Mina2Thread2 extends Thread {
    
    @Override
    public void run() {
        try {
            new Mina2().reloadResource();
        } catch (InterruptedException ex) {
        }
    }
}

Hello
done 1
Hello
done 2
done 1
done 2
done 1
done 2
done 1
done 2

你可能感兴趣的:(同步类)