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

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

直接po代码和截图

源码点这里

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

运行结果,情况2

运行结果,情况3

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

运行结果,情况2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值