线程同时存在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挂起唤醒。