Java基础 线程8锁
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone=new Phone();
new Thread(()->{
phone.sendMes();
}) .start();
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}) .start();
}
}
class Phone{
public synchronized void sendMes() {
System.out.println("sendMes");
}
public synchronized void call() {
System.out.println("call");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone=new Phone();
new Thread(()->{
phone.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone{
public synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public synchronized void call(){
System.out.println("call");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone1=new Phone();
Phone phone2=new Phone();
new Thread(()->{
phone1.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
}).start();
}
}
class Phone{
public synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public synchronized void call(){
System.out.println("call");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone=new Phone();
new Thread(()->{
phone.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.hello();
}).start();
}
}
class Phone{
public synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public synchronized void call(){
System.out.println("call");
}
public void hello(){
System.out.println("hello ");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone=new Phone();
new Thread(()->{
phone.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone{
public static synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public static synchronized void call(){
System.out.println("call");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone1=new Phone();
Phone phone2=new Phone();
new Thread(()->{
phone1.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
}).start();
}
}
class Phone{
public static synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public static synchronized void call(){
System.out.println("call");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone=new Phone();
new Thread(()->{
phone.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone{
public static synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public synchronized void call(){
System.out.println("call");
}
}
package com.robot;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
Phone phone1=new Phone();
Phone phone2=new Phone();
new Thread(()->{
phone1.sendMes();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
}).start();
}
}
class Phone{
public static synchronized void sendMes(){
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendMes");
}
public synchronized void call(){
System.out.println("call");
}
}