java中的同步关键字,java线程中的synchronized同步关键字和sleep()函数

java线程中的synchronized同步关键字和sleep()函数

直接po代码和截图

5b03230603be522a9f0d13540edd62b0.png

Student类

package com.demo.thread4;

public class Student{

public synchronized void hello1() {

System.out.println("我是同步方法hello1()******");

try {

Thread.sleep(4000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("sleep(4000)结束了,我是同步方法hello1()的结尾部分........");

}

public synchronized void hello2() {

System.out.println("我是同步方法hello2()##################");

}

public void hello3() {

System.out.println("我是普通方法hello3()------------------------");

}

public void hello4() {

System.out.println("我是普通方法hello4()=======");

try {

Thread.sleep(4000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("sleep(4000)结束了,我是普通方法hello4()的结尾部分!!!!!!!!!!!");

}

}

Family1类

package com.demo.thread4;

public class Family1 implements Runnable{

Student student;

public Family1(Student student) {

this.student = student;

}

@Override

public void run() {

test();

}

public void test() {

student.hello1();

}

}

Family2类

package com.demo.thread4;

public class Family2 implements Runnable{

Student student;

public Family2(Student student) {

this.student = student;

}

@Override

public void run() {

test();

}

public void test() {

student.hello2();

}

}

Family3类

package com.demo.thread4;

public class Family3 implements Runnable{

Student student;

public Family3(Student student) {

this.student = student;

}

@Override

public void run() {

test();

}

public void test() {

student.hello3();

}

}

Family4类

package com.demo.thread4;

public class Family4 implements Runnable{

Student student;

public Family4(Student student) {

this.student = student;

}

@Override

public void run() {

test();

}

public void test() {

student.hello4();

}

}

Test类

package com.demo.thread4;

public class Test {

public static void main(String[] args) {

Student student = new Student();

Family1 family1 = new Family1(student);

Family2 family2 = new Family2(student);

Family3 family3 = new Family3(student);

Thread thread1 = new Thread(family1, "线程1");

Thread thread2 = new Thread(family2, "线程2");

Thread thread3 = new Thread(family3, "线程3");

thread1.start();

thread2.start();

thread3.start();

}

}

运行结果:可以多运行几次

运行结果,情况1

1d7c3f1adbefdfc08036274c642edcbf.png

运行结果,情况2

c29cbeaf8694f8a21985703686d2ef5c.png

运行结果,情况3

5a8ae86f7bec9cb9edaefb13e59102b4.png

Test2类

package com.demo.thread4;

public class Test2 {

public static void main(String[] args) {

Student student = new Student();

Family4 family4 = new Family4(student);

Family2 family2 = new Family2(student);

Family3 family3 = new Family3(student);

Thread t1 = new Thread(family4, "线程1");

Thread t2 = new Thread(family2, "线程2");

Thread t3 = new Thread(family3, "线程3");

t1.start();

t2.start();

t3.start();

}

}

运行结果:可以多运行几次

运行结果,情况1

11dbc07062ce3886bf38cbe3b4845657.png

运行结果,情况2

ec476435e38593b098171d257ab2c9bd.png

标签:java,student,synchronized,void,Thread,线程,Student,new,public

来源: https://blog.csdn.net/czh500/article/details/88922133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值