8 locks, 8 questions about locks
1. Two common synchronization methods, one object, two threads, print and send text messages or make calls first?
import java.util.concurrent.TimeUnit;
/**
* 1.两个普通的同步方法,一个对象,两个线程先打印发短信还是打电话? -- 1.发短信 2.打电话
*/
public class Test01 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendSMS();
},"A").start();
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone{
//synchronized 锁的对象是方法的调用者
//两个方法用的同一个锁,谁先拿到锁谁就先执行
public synchronized void sendSMS(){
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
}
2. Two ordinary synchronization methods, one object, send a text message and sleep for 4s, the two threads first print and send a text message or make a call?
import java.util.concurrent.TimeUnit;
/**
* 2.两个普通的同步方法,一个对象,发短信休眠4s,两个线程先打印发短信还是打电话? -- 1.发短信 2.打电话
*/
public class Test01 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendSMS();
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone{
//synchronized 锁的对象是方法的调用者
//两个方法用的同一个锁,谁先拿到锁谁就先执行
public synchronized void sendSMS(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
}
3. An ordinary method (non-synchronized), an ordinary synchronous method, an object, two threads, print and send text messages or hello first?
import java.util.concurrent.TimeUnit;
/**
* 3.一个普通方法(非同步),一个普通的同步方法,一个对象,两个线程先打印发短信还是hello? -- 1.hello 2.发短信
*/
public class Test02 {
public static void main(String[] args) {
Phone2 phone = new Phone2();
new Thread(()->{
phone.sendSMS();
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.hello();
},"B").start();
}
}
class Phone2{
//synchronized 锁的对象是方法的调用者
//两个方法用的同一个锁,谁先拿到锁谁就先执行
public synchronized void sendSMS(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
//这里没有锁!不是同步方法块,不受锁的影响
public void hello(){
System.out.println("你好");
}
}
4. Two synchronization methods, two objects, two threads, print and send text messages or make calls first?
import java.util.concurrent.TimeUnit;
/**
* 4.两个同步方法,两个对象, 两个线程先打印发短信还是打电话 -- 1.打电话 2.发短信
*/
public class Test03 {
public static void main(String[] args) {
//2个对象,2个调用者,两把锁
Phone3 phone1 = new Phone3();
Phone3 phone2 = new Phone3();
new Thread(()->{
phone1.sendSMS();
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone3{
//synchronized 锁的对象是方法的调用者
//两个方法用的同一个锁,谁先拿到锁谁就先执行
public synchronized void sendSMS(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
//这里没有锁!不是同步方法块,不受锁的影响
public void hello(){
System.out.println("你好");
}
}
5. Two static synchronization methods, one object, and two threads, print and send text messages or make calls first?
import java.util.concurrent.TimeUnit;
/**
* 5.两个静态的同步方法,一个对象,两个线程先打印发短信还是打电话 -- 1.发短信 2.打电话
*/
public class Test04 {
public static void main(String[] args) {
Phone4 phone = new Phone4();
new Thread(()->{
phone.sendSMS();
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone4{
//synchronized 锁的对象是方法的调用者
//static 静态方法
//类一加载就有了,锁的是Class
public static synchronized void sendSMS(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public static synchronized void call(){
System.out.println("打电话");
}
}
6. Two static synchronization methods, two objects, two threads, print and send text messages or make calls first?
/**
* 6.两个静态的同步方法,两个对象,两个线程先打印发短信还是打电话 -- 1.发短信 2.打电话
*/
public class Test05 {
public static void main(String[] args) {
//连个对象的Class类模板只有一个,static,锁的是Class
Phone5 phone1 = new Phone5();
Phone5 phone2 = new Phone5();
new Thread(()->{
phone1.sendSMS();
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
//Phone5 唯一的一个Class对象
class Phone5{
//synchronized 锁的对象是方法的调用者
//static 静态方法
//类一加载就有了,锁的是Class
public static synchronized void sendSMS(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public static synchronized void call(){
System.out.println("打电话");
}
}
7. A static synchronization method, an ordinary synchronization method, an object, and two threads. Print and send text messages or make calls first?
import java.util.concurrent.TimeUnit;
/**
* 7. 一个静态同步方法,一个普通的同步方法,一个对象,两个线程先打印发短信还是打电话--1.打电话 2.发短信
*/
public class Test06 {
public static void main(String[] args) {
//连个对象的Class类模板只有一个,static,锁的是Class
Phone6 phone = new Phone6();
new Thread(()->{
phone.sendSMS();
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
//Phone6 唯一的一个Class对象
class Phone6{
//synchronized 锁的对象是方法的调用者
//static 静态方法
//类一加载就有了,锁的是Class
public static synchronized void sendSMS(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
//普通的同步方法,锁的是调用者
public synchronized void call(){
System.out.println("打电话");
}
}
8. A static synchronization method, an ordinary synchronization method, two objects, two threads, print and send text messages or make phone calls first?
import java.util.concurrent.TimeUnit;
/**
* 8. 一个静态同步方法,一个普通的同步方法,两个对象,两个线程先打印发短信还是打电话--1.打电话 2.发短信
*/
public class Test07 {
public static void main(String[] args) {
//连个对象的Class类模板只有一个,static,锁的是Class
Phone7 phone1 = new Phone7();
Phone7 phone2 = new Phone7();
new Thread(() -> {
phone1.sendSMS();
}, "A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone2.call();
}, "B").start();
}
}
//Phone3 唯一的一个Class对象
class Phone7 {
//静态的同步方法,锁的是Class
public static synchronized void sendSMS() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
//普通的同步方法,锁的是调用者
public synchronized void call() {
System.out.println("打电话");
}
}
summary
new this specifically a mobile phone
The only template for static Class