视频链接
import java.util.concurrent.TimeUnit;
public class test02 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
phone.sendSms();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e)
{
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone{
public synchronized void sendSms(){
System.out.println("sendsms");
}
public synchronized void call(){
System.out.println("call");
}
}
3.4
import java.util.concurrent.TimeUnit;
public class test03 {
public static void main(String[] args) throws InterruptedException {
Phone2 phone = new Phone2();
Phone2 phone2 = new Phone2();
new Thread(()->{
phone.sendSms();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e)
{
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone2{
public synchronized void sendSms(){
System.out.println("sendsms");
}
public synchronized void call(){
System.out.println("call");
}
public void hello(){
System.out.println("hello");
}
}
5.6
import java.util.concurrent.TimeUnit;
public class test04 {
public static void main(String[] args) throws InterruptedException {
Phone3 phone = new Phone3();
Phone3 phone2 = new Phone3();
new Thread(()->{
phone.sendSms();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e)
{
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone3{
public static synchronized void sendSms(){
System.out.println("sendsms");
}
public static synchronized void call(){
System.out.println("call");
}
public void hello(){
System.out.println("hello");
}
}
7.8
import java.util.concurrent.TimeUnit;
public class test05 {
public static void main(String[] args) throws InterruptedException {
Phone4 phone = new Phone4();
Phone4 phone2 = new Phone4();
new Thread(()->{
phone.sendSms();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e)
{
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone4{
public static synchronized void sendSms(){
System.out.println("sendsms");
}
public synchronized void call(){
System.out.println("call");
}
public void hello(){
System.out.println("hello");
}
}