线程八锁以及代码实现

题目:判断打印的 “one” or “two” ?

    1. 两个普通同步方法,两个线程,标准打印, 打印? //one two
    1. 新增 Thread.sleep() 给 getOne() ,打印? //one two
    1. 新增普通方法 getThree() , 打印? //three one two
    1. 两个普通同步方法,两个 Number 对象,打印? //two one
    1. 修改 getOne() 为静态同步方法,打印? //two one
    1. 修改两个方法均为静态同步方法,一个 Number 对象? //one two
    1. 一个静态同步方法,一个非静态同步方法,两个 Number 对象? //two one
    1. 两个静态同步方法,两个 Number 对象? //one two

// 1. 两个普通同步方法,两个线程,标准打印, 打印? //one two
// 2. 新增 Thread.sleep() 给 getOne() ,打印? //one two


public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }
        }).start();
    }

}
class Demo{
    public synchronized void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
}

1、2:同步方法的所对象是this,先调用的方法先执行,虽然是两个线程,但是只能有一个同步方法被执行。

// 3. 新增普通方法 getThree() , 打印? //three one two

public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();
    }

}
class Demo{
    public synchronized void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
    public void getThree(){
        System.out.println("Three");
    }
}

3.同步方法不受锁的约束

 // 4. 两个普通同步方法,两个 Number 对象,打印?  //two  one
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        Demo demo1=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo1.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

4.形成线程同步的前提是,多个线程拥有同一把锁,本题创建了两个对象,相当于两把锁了。故:各自执行各自的

//5. 修改 getOne() 为静态同步方法,打印?  //two   one
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

5.静态的同步方法的锁是类实例,同步方法的锁是this,两个线程拥有不同的锁,所以实现不了同步

// * 6. 修改两个方法均为静态同步方法,一个 Number 对象?  //one   two
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized static void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

6.都是静态方法,公用一个对象锁(类实力),故可以实现同步

//* 7. 一个静态同步方法,一个非静态同步方法,两个 Number 对象?  //two  one
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        Demo demo1=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo1.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized  void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

7.不是同一个锁,故不同步

// * 8. 两个静态同步方法,两个 Number 对象?   //one  two
public class One {
    public static void main(String[] args) {
        Demo demo=new Demo();
        Demo demo1=new Demo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getOne();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                demo1.getTwo();
            }

       }).start();
         /*new Thread(new Runnable() {
            @Override
            public void run() {
                demo.getThree();
            }
        }).start();*/

    }

}
class Demo{
    public synchronized static void getOne(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("one");
    }
    public synchronized static void getTwo(){
        System.out.println("two");
    }
  /*  public void getThree(){
        System.out.println("Three");
    }*/
}

8.静态同步方法的锁是“类实例”,与多少个对象无关

总结:通过上述的代码,可以得出:

线程八锁以及代码实现_第1张图片
线程八锁以及代码实现_第2张图片

你可能感兴趣的:(Java基础学法学习)