美文网首页
java . 上锁

java . 上锁

作者: abaeccdce206 | 来源:发表于2018-07-09 16:08 被阅读4次

    import org.testng.annotations.Test;

    public class SynchronizedTest {

    public synchronized void test() {
        System.out.println("test开始.........");
        try {
            Thread.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("test结束!!!!!!!!!!");
    }
    
    
    
    public  void test2() {
        synchronized (SynchronizedTest.class) {
            System.out.println("test2开始.........");
            try {
                Thread.sleep(1000);
    
            } catch (InterruptedException e) {
                System.out.println(e);
                e.printStackTrace();
            }
            System.out.println("test2结束.........");
        }
    }
    

    }

    结果
    test开始.........
    test开始.........
    test开始.........
    test结束!!!!!!!!!!
    test结束!!!!!!!!!!
    test结束!!!!!!!!!!

    ================

    test2开始.........
    test2结束.........
    test2开始.........
    test2结束.........
    test2开始.........
    test2结束.........

    Process finished with exit code 0

    相关文章

      网友评论

          本文标题:java . 上锁

          本文链接:https://www.haomeiwen.com/subject/fnltpftx.html