Runnable,Thread.run,wait,notify

线程同时存在Runable和Thread的run方法先执行哪一个?先看源码

    /**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object's <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> should override this method.
     *
     * @see     #start()
     * @see     #stop()
     * @see     #Thread(ThreadGroup, Runnable, String)
     */
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

先一开始判断run有没被重写,没被重写再调用runnable里面的重写

示例:

public class Demo2 {
	
public static void main(String[] args) {
	//Runnable为空,直接执行run方法的空代码,这里重写run,变相执行run的新代码方法
	Thread thread = new Thread() {
		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName());
		}
	};
	thread.start();
	
	//Runnable不为空,直接执行Runnable里面的run方法代替
	Thread thread2 = new Thread(new Runnable() {
		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName());		
		}
	});
	thread2.start();
	
	//在run没有被重写时才判断Runnable是不是空,如果重写了run就马上运行run
	Thread thread3 = new Thread(new Runnable() {
		@Override
	public void run() {
			System.out.println("1"+Thread.currentThread().getName());	
		
	}}) {
		@Override
		public void run() {
			System.out.println("2"+Thread.currentThread().getName());
			//如果不调用这句就不会调用Runnable里面的内容,因为run在这里被重写了,下面这句是调用了超类的run,所以会检测存在不存在target
			super.run();
		}
	};
	thread3.start();
}
}

就好像Thread3的情况

我们看看结果

Thread-0
Thread-1
2Thread-2
1Thread-2

结果证明先执行Run重写再判断有没Runnable

在Thread3 中,同样也重写了Thread的run()方法,同时传入了一个Runnable对象,实现了run()方法。唯一不同的是,在Thread重写的run方法中,在打印输出后,还执行了super.run(),这就有意思了。

首先,该线程启动运行后,执行其重写的run()方法,输出2Thread-2。

接下来调用super.run(),也就是调用超类的run()方法,而该超类的run()方法,也就是JDK定义的Thread类的run(),其执行如上代码段 3 所示;显然target不为空,这时候会调用该对象的run()方法,会输1Thread-2.。


wait,notify的使用方法,通过wait,notify来控制各线程执行一次

synchronized在方法上

public class Demo7 {
	int a =0;
	boolean isWho =true;
public synchronized void sub() {
	if(isWho) {
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	System.out.println("sub"+" "+a);
	 a =a+1;
	isWho=true;
	this.notify();
}

public synchronized void plus() {
	if(!isWho) {
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	System.out.println("plus"+" "+a);
	 a = a+1;
	isWho=false;
	this.notify();
}

public static void main(String[] args) {
	Demo7 demo = new Demo7();
	Thread thread = new Thread(new Runnable() {
		@Override
		public void run() {
			for(int i=0;i<10;i++) {
			demo.sub();
			}
		}
	});
	thread.start();
	Thread thread2 = new Thread(new Runnable() {
		
		@Override
		public void run() {
			for(int i=0;i<10;i++) {
			demo.plus();
			}
		}
	});
	thread2.start();
	
}


}

执行结果:

plus 0
sub 1
plus 2
sub 3
plus 4
sub 5
plus 6
sub 7
plus 8
sub 9
plus 10
sub 11
plus 12
sub 13
plus 14
sub 15
plus 16
sub 17
plus 18
sub 19

synchronized代码块示例:

public class Demo2 {

	public static void main(String[] args) {
		Amd amd = new Amd();

		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 100; i++) {
					synchronized (amd) {
						if (amd.b) {
							try {
								// 挂起不再往下执行,用this的话,所控制的锁和amd不一样,所以实际上没实现到挂起和释放的功能,this和amd的实际地址不一样,synchronize在方法上本来就默认指this,所以可以用this挂起唤醒
								amd.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						amd.plus();
						// 变成true就不能继续执行这个了
						amd.b = true;
						// 唤醒让下面执行
						amd.notify();
					}
				}
			}
		});
		thread1.start();

		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 100; i++) {
					synchronized (amd) {
						if (!amd.b) {
							try {
								amd.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						amd.plus();
						amd.b = false;
						amd.notify();
					}
				}
			}
		});
		thread2.start();
	}
}
public class Amd {
int a = 0;
boolean b= true;
public void plus() {
	System.out.println(Thread.currentThread().getName()+"现在的钱:"+a);
	a=a+5;
	System.out.println(Thread.currentThread().getName()+"存钱后:"+a);
}

public void sub() {
	System.out.println(Thread.currentThread().getName()+"现在的钱:"+a);
	a=a+5;
	System.out.println(Thread.currentThread().getName()+"存钱后:"+a);
}
}

执行结果:

Thread-1现在的钱:0
Thread-1存钱后:5
Thread-0现在的钱:5
Thread-0存钱后:10
Thread-1现在的钱:10
Thread-1存钱后:15
Thread-0现在的钱:15
Thread-0存钱后:20
Thread-1现在的钱:20
Thread-1存钱后:25
Thread-0现在的钱:25
Thread-0存钱后:30
Thread-1现在的钱:30
Thread-1存钱后:35
Thread-0现在的钱:35
Thread-0存钱后:40
Thread-1现在的钱:40
Thread-1存钱后:45
Thread-0现在的钱:45
Thread-0存钱后:50
Thread-1现在的钱:50
Thread-1存钱后:55
Thread-0现在的钱:55
Thread-0存钱后:60
Thread-1现在的钱:60
Thread-1存钱后:65
Thread-0现在的钱:65
Thread-0存钱后:70
Thread-1现在的钱:70
Thread-1存钱后:75
Thread-0现在的钱:75
Thread-0存钱后:80
Thread-1现在的钱:80
Thread-1存钱后:85
Thread-0现在的钱:85
Thread-0存钱后:90
Thread-1现在的钱:90
Thread-1存钱后:95
Thread-0现在的钱:95
Thread-0存钱后:100
Thread-1现在的钱:100
Thread-1存钱后:105
Thread-0现在的钱:105
Thread-0存钱后:110
Thread-1现在的钱:110
Thread-1存钱后:115
Thread-0现在的钱:115
Thread-0存钱后:120
Thread-1现在的钱:120
Thread-1存钱后:125
Thread-0现在的钱:125
Thread-0存钱后:130
Thread-1现在的钱:130
Thread-1存钱后:135
Thread-0现在的钱:135
Thread-0存钱后:140
Thread-1现在的钱:140
Thread-1存钱后:145
Thread-0现在的钱:145
Thread-0存钱后:150
Thread-1现在的钱:150
Thread-1存钱后:155
Thread-0现在的钱:155
Thread-0存钱后:160
Thread-1现在的钱:160
Thread-1存钱后:165
Thread-0现在的钱:165
Thread-0存钱后:170
Thread-1现在的钱:170
Thread-1存钱后:175
Thread-0现在的钱:175
Thread-0存钱后:180
Thread-1现在的钱:180
Thread-1存钱后:185
Thread-0现在的钱:185
Thread-0存钱后:190
Thread-1现在的钱:190
Thread-1存钱后:195
Thread-0现在的钱:195
Thread-0存钱后:200
Thread-1现在的钱:200
Thread-1存钱后:205
Thread-0现在的钱:205
Thread-0存钱后:210
Thread-1现在的钱:210
Thread-1存钱后:215
Thread-0现在的钱:215
Thread-0存钱后:220
Thread-1现在的钱:220
Thread-1存钱后:225
Thread-0现在的钱:225
Thread-0存钱后:230
Thread-1现在的钱:230
Thread-1存钱后:235
Thread-0现在的钱:235
Thread-0存钱后:240
Thread-1现在的钱:240
Thread-1存钱后:245
Thread-0现在的钱:245
Thread-0存钱后:250
Thread-1现在的钱:250
Thread-1存钱后:255
Thread-0现在的钱:255
Thread-0存钱后:260
Thread-1现在的钱:260
Thread-1存钱后:265
Thread-0现在的钱:265
Thread-0存钱后:270
Thread-1现在的钱:270
Thread-1存钱后:275
Thread-0现在的钱:275
Thread-0存钱后:280
Thread-1现在的钱:280
Thread-1存钱后:285
Thread-0现在的钱:285
Thread-0存钱后:290
Thread-1现在的钱:290
Thread-1存钱后:295
Thread-0现在的钱:295
Thread-0存钱后:300
Thread-1现在的钱:300
Thread-1存钱后:305
Thread-0现在的钱:305
Thread-0存钱后:310
Thread-1现在的钱:310
Thread-1存钱后:315
Thread-0现在的钱:315
Thread-0存钱后:320
Thread-1现在的钱:320
Thread-1存钱后:325
Thread-0现在的钱:325
Thread-0存钱后:330
Thread-1现在的钱:330
Thread-1存钱后:335
Thread-0现在的钱:335
Thread-0存钱后:340
Thread-1现在的钱:340
Thread-1存钱后:345
Thread-0现在的钱:345
Thread-0存钱后:350
Thread-1现在的钱:350
Thread-1存钱后:355
Thread-0现在的钱:355
Thread-0存钱后:360
Thread-1现在的钱:360
Thread-1存钱后:365
Thread-0现在的钱:365
Thread-0存钱后:370
Thread-1现在的钱:370
Thread-1存钱后:375
Thread-0现在的钱:375
Thread-0存钱后:380
Thread-1现在的钱:380
Thread-1存钱后:385
Thread-0现在的钱:385
Thread-0存钱后:390
Thread-1现在的钱:390
Thread-1存钱后:395
Thread-0现在的钱:395
Thread-0存钱后:400
Thread-1现在的钱:400
Thread-1存钱后:405
Thread-0现在的钱:405
Thread-0存钱后:410
Thread-1现在的钱:410
Thread-1存钱后:415
Thread-0现在的钱:415
Thread-0存钱后:420
Thread-1现在的钱:420
Thread-1存钱后:425
Thread-0现在的钱:425
Thread-0存钱后:430
Thread-1现在的钱:430
Thread-1存钱后:435
Thread-0现在的钱:435
Thread-0存钱后:440
Thread-1现在的钱:440
Thread-1存钱后:445
Thread-0现在的钱:445
Thread-0存钱后:450
Thread-1现在的钱:450
Thread-1存钱后:455
Thread-0现在的钱:455
Thread-0存钱后:460
Thread-1现在的钱:460
Thread-1存钱后:465
Thread-0现在的钱:465
Thread-0存钱后:470
Thread-1现在的钱:470
Thread-1存钱后:475
Thread-0现在的钱:475
Thread-0存钱后:480
Thread-1现在的钱:480
Thread-1存钱后:485
Thread-0现在的钱:485
Thread-0存钱后:490
Thread-1现在的钱:490
Thread-1存钱后:495
Thread-0现在的钱:495
Thread-0存钱后:500
Thread-1现在的钱:500
Thread-1存钱后:505
Thread-0现在的钱:505
Thread-0存钱后:510
Thread-1现在的钱:510
Thread-1存钱后:515
Thread-0现在的钱:515
Thread-0存钱后:520
Thread-1现在的钱:520
Thread-1存钱后:525
Thread-0现在的钱:525
Thread-0存钱后:530
Thread-1现在的钱:530
Thread-1存钱后:535
Thread-0现在的钱:535
Thread-0存钱后:540
Thread-1现在的钱:540
Thread-1存钱后:545
Thread-0现在的钱:545
Thread-0存钱后:550
Thread-1现在的钱:550
Thread-1存钱后:555
Thread-0现在的钱:555
Thread-0存钱后:560
Thread-1现在的钱:560
Thread-1存钱后:565
Thread-0现在的钱:565
Thread-0存钱后:570
Thread-1现在的钱:570
Thread-1存钱后:575
Thread-0现在的钱:575
Thread-0存钱后:580
Thread-1现在的钱:580
Thread-1存钱后:585
Thread-0现在的钱:585
Thread-0存钱后:590
Thread-1现在的钱:590
Thread-1存钱后:595
Thread-0现在的钱:595
Thread-0存钱后:600
Thread-1现在的钱:600
Thread-1存钱后:605
Thread-0现在的钱:605
Thread-0存钱后:610
Thread-1现在的钱:610
Thread-1存钱后:615
Thread-0现在的钱:615
Thread-0存钱后:620
Thread-1现在的钱:620
Thread-1存钱后:625
Thread-0现在的钱:625
Thread-0存钱后:630
Thread-1现在的钱:630
Thread-1存钱后:635
Thread-0现在的钱:635
Thread-0存钱后:640
Thread-1现在的钱:640
Thread-1存钱后:645
Thread-0现在的钱:645
Thread-0存钱后:650
Thread-1现在的钱:650
Thread-1存钱后:655
Thread-0现在的钱:655
Thread-0存钱后:660
Thread-1现在的钱:660
Thread-1存钱后:665
Thread-0现在的钱:665
Thread-0存钱后:670
Thread-1现在的钱:670
Thread-1存钱后:675
Thread-0现在的钱:675
Thread-0存钱后:680
Thread-1现在的钱:680
Thread-1存钱后:685
Thread-0现在的钱:685
Thread-0存钱后:690
Thread-1现在的钱:690
Thread-1存钱后:695
Thread-0现在的钱:695
Thread-0存钱后:700
Thread-1现在的钱:700
Thread-1存钱后:705
Thread-0现在的钱:705
Thread-0存钱后:710
Thread-1现在的钱:710
Thread-1存钱后:715
Thread-0现在的钱:715
Thread-0存钱后:720
Thread-1现在的钱:720
Thread-1存钱后:725
Thread-0现在的钱:725
Thread-0存钱后:730
Thread-1现在的钱:730
Thread-1存钱后:735
Thread-0现在的钱:735
Thread-0存钱后:740
Thread-1现在的钱:740
Thread-1存钱后:745
Thread-0现在的钱:745
Thread-0存钱后:750
Thread-1现在的钱:750
Thread-1存钱后:755
Thread-0现在的钱:755
Thread-0存钱后:760
Thread-1现在的钱:760
Thread-1存钱后:765
Thread-0现在的钱:765
Thread-0存钱后:770
Thread-1现在的钱:770
Thread-1存钱后:775
Thread-0现在的钱:775
Thread-0存钱后:780
Thread-1现在的钱:780
Thread-1存钱后:785
Thread-0现在的钱:785
Thread-0存钱后:790
Thread-1现在的钱:790
Thread-1存钱后:795
Thread-0现在的钱:795
Thread-0存钱后:800
Thread-1现在的钱:800
Thread-1存钱后:805
Thread-0现在的钱:805
Thread-0存钱后:810
Thread-1现在的钱:810
Thread-1存钱后:815
Thread-0现在的钱:815
Thread-0存钱后:820
Thread-1现在的钱:820
Thread-1存钱后:825
Thread-0现在的钱:825
Thread-0存钱后:830
Thread-1现在的钱:830
Thread-1存钱后:835
Thread-0现在的钱:835
Thread-0存钱后:840
Thread-1现在的钱:840
Thread-1存钱后:845
Thread-0现在的钱:845
Thread-0存钱后:850
Thread-1现在的钱:850
Thread-1存钱后:855
Thread-0现在的钱:855
Thread-0存钱后:860
Thread-1现在的钱:860
Thread-1存钱后:865
Thread-0现在的钱:865
Thread-0存钱后:870
Thread-1现在的钱:870
Thread-1存钱后:875
Thread-0现在的钱:875
Thread-0存钱后:880
Thread-1现在的钱:880
Thread-1存钱后:885
Thread-0现在的钱:885
Thread-0存钱后:890
Thread-1现在的钱:890
Thread-1存钱后:895
Thread-0现在的钱:895
Thread-0存钱后:900
Thread-1现在的钱:900
Thread-1存钱后:905
Thread-0现在的钱:905
Thread-0存钱后:910
Thread-1现在的钱:910
Thread-1存钱后:915
Thread-0现在的钱:915
Thread-0存钱后:920
Thread-1现在的钱:920
Thread-1存钱后:925
Thread-0现在的钱:925
Thread-0存钱后:930
Thread-1现在的钱:930
Thread-1存钱后:935
Thread-0现在的钱:935
Thread-0存钱后:940
Thread-1现在的钱:940
Thread-1存钱后:945
Thread-0现在的钱:945
Thread-0存钱后:950
Thread-1现在的钱:950
Thread-1存钱后:955
Thread-0现在的钱:955
Thread-0存钱后:960
Thread-1现在的钱:960
Thread-1存钱后:965
Thread-0现在的钱:965
Thread-0存钱后:970
Thread-1现在的钱:970
Thread-1存钱后:975
Thread-0现在的钱:975
Thread-0存钱后:980
Thread-1现在的钱:980
Thread-1存钱后:985
Thread-0现在的钱:985
Thread-0存钱后:990
Thread-1现在的钱:990
Thread-1存钱后:995
Thread-0现在的钱:995
Thread-0存钱后:1000

等待通知机制,是指一个线程A调用了对象O的wait()方法进入等待状态,而另一个线程B调用了对象O的notify()或者notifyAll()方法,线程A收到通知后从对象O的wait()方法返回,进而执行后续操作。上述两个线程通过对象O来完成交互,而对象上的wait()和notify/notifyAll()的关系就如同开关信号一样,用来完成等待方和通知方之间的交互工作。

 amd的挂起不再往下执行,用this的话,所控制的锁和amd不一样,所以实际上没实现到挂起和释放的功能,this和amd的实际地址不一样,synchronize在方法上本来就默认指this,所以可以用this挂起唤醒。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`wait()` 和 `notify()` 方法是 Java 中用于线程间通信的两个重要方法。`wait()` 方法用于让线程进入等待状态,并释放锁,而 `notify()` 方法则用于唤醒等待状态的线程。这两个方法必须在同步方法或同步块中使用,否则会抛出 `IllegalMonitorStateException` 异常。 `wait()` 方法的调用会让线程进入等待状态,直到有其他线程调用了同一个对象的 `notify()` 或 `notifyAll()` 方法来唤醒它。在等待期间,线程会释放它持有的锁,以便其他线程可以进入同步块执行。`wait()` 方法可以使用以下两种方式调用: - `wait()`:让线程一直等待,直到其他线程调用了 `notify()` 或 `notifyAll()` 方法唤醒它。 - `wait(long timeout)`:让线程等待一段时间,如果在等待期间没有其他线程调用 `notify()` 或 `notifyAll()` 方法唤醒它,那么线程会自动醒来。 `notify()` 方法用于唤醒等待状态的线程。它会随机地唤醒一个等待状态的线程,如果有多个线程都在等待同一个对象的锁,那么只有其中一个线程会被唤醒。`notifyAll()` 方法则会唤醒所有等待状态的线程。 以下是一个简单的示例,展示了如何使用 `wait()` 和 `notify()` 方法进行线程间通信: ```java class MyThread implements Runnable { private final Object lock; public MyThread(Object lock) { this.lock = lock; } @Override public void run() { synchronized (lock) { System.out.println("Thread " + Thread.currentThread().getName() + " is waiting"); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + Thread.currentThread().getName() + " is awake"); } } } public class Main { public static void main(String[] args) throws InterruptedException { Object lock = new Object(); Thread t1 = new Thread(new MyThread(lock)); Thread t2 = new Thread(new MyThread(lock)); t1.start(); t2.start(); Thread.sleep(1000); synchronized (lock) { lock.notify(); } } } ``` 在这个示例中,我们创建了两个线程 `t1` 和 `t2`,它们都在同一个对象 `lock` 上等待。在主线程中,我们等待 1 秒钟后调用了 `notify()` 方法来唤醒一个等待状态的线程。由于 `notify()` 方法是随机唤醒一个线程,因此我们无法确定哪个线程会被唤醒。在这个示例中,我们可以看到其中一个线程被唤醒并输出了 "Thread X is awake" 的信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值