1.标准访问:先发送邮件后发送短信
2.暂停3s:先发送邮件后发送短信
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone.sendMessage();
},"B").start();
}
}
class Phone{
public synchronized void sendMessage(){
System.out.println("sendMsg");
}
public synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
}
3.新增普通方法sendQQ():先发送QQ后发送邮件
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone.sendQQ();
},"B").start();
}
}
class Phone{
public synchronized void sendMessage(){
System.out.println("sendMsg");
}
public synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public void sendQQ(){
System.out.println("sendQQ");
}
}
4.两个phone:先发送短信后发送邮件
public class ThreadDemo6 {
public static void main(String[] args) throws InterruptedException {
Phone phone1 = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
try {
phone1.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone2.sendMessage();
},"B").start();
}
}
5.两个静态同步方法,同一部手机:先发送邮件后发送短信
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone.sendMessage();
},"B").start();
}
}
class Phone{
public static synchronized void sendMessage(){
System.out.println("sendMsg");
}
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
}
6.两个静态同步方法,两部手机:先发送邮件后发送短信
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
try {
phone.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone2.sendMessage();
},"B").start();
}
}
class Phone{
public static synchronized void sendMessage(){
System.out.println("sendMsg");
}
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
}
7.一个静态同步方法,一个普通同步方法,同一部手机:先发送短信后发送邮件
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone.sendMessage();
},"B").start();
}
}
class Phone{
public synchronized void sendMessage(){
System.out.println("sendMsg");
}
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
}
8.一个静态同步方法,一个普通同步方法,两部手机:先发送短信后发送邮件
public class ThreadDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
try {
phone.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
Thread.sleep(1000);
new Thread(()->{
phone2.sendMessage();
},"B").start();
}
}
class Phone{
public synchronized void sendMessage(){
System.out.println("sendMsg");
}
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
}