Java 线程的start()方法和run()方法的区别

先看下通俗的定义区别

新建MyThread线程

package chapter01.section02.thread_1_2_1.project_3_z;

public class MyThread extends Thread{
	
	private int i;

	public MyThread(int i) {
		super();
		this.i = i;
	}

	@Override
	public void run() {
		System.out.println(i);
	}
	
}

 

start()方法执行效果

package chapter01.section02.thread_1_2_1.project_3_z;

public class Test {
	
	public static void main(String[] args) {
		MyThread t11 = new MyThread(1);
		MyThread t12 = new MyThread(2);
		MyThread t13 = new MyThread(3);
		MyThread t14 = new MyThread(4);
		MyThread t15 = new MyThread(5);
		MyThread t16 = new MyThread(6);
		MyThread t17 = new MyThread(7);
		MyThread t18 = new MyThread(8);
		MyThread t19 = new MyThread(9);
		MyThread t110 = new MyThread(10);
		MyThread t111 = new MyThread(11);
		MyThread t112 = new MyThread(12);
		MyThread t113 = new MyThread(13);

		t11.start();
		t12.start();
		t13.start();
		t14.start();
		t15.start();
		t16.start();
		t17.start();
		t18.start();
		t19.start();
		t110.start();
		t111.start();
		t112.start();
		t113.start();

	}
	
}

start()执行结果【执行随机】

2
5
4
1
3
7
6
10
9
12
13
8
11

 

run()方法代码

package chapter01.section02.thread_1_2_1.project_3_z;

public class Test {
	
	public static void main(String[] args) {
		MyThread t11 = new MyThread(1);
		MyThread t12 = new MyThread(2);
		MyThread t13 = new MyThread(3);
		MyThread t14 = new MyThread(4);
		MyThread t15 = new MyThread(5);
		MyThread t16 = new MyThread(6);
		MyThread t17 = new MyThread(7);
		MyThread t18 = new MyThread(8);
		MyThread t19 = new MyThread(9);
		MyThread t110 = new MyThread(10);
		MyThread t111 = new MyThread(11);
		MyThread t112 = new MyThread(12);
		MyThread t113 = new MyThread(13);

		t11.run();
		t12.run();
		t13.run();
		t14.run();
		t15.run();
		t16.run();
		t17.run();
		t18.run();
		t19.run();
		t110.run();
		t111.run();
		t112.run();
		t113.run();

	}
	
}

 

run()方法执行结果【顺序执行】

1
2
3
4
5
6
7
8
9
10
11
12
13

 

上述代码援引:https://github.com/burnszp/java-multi-thread-programming

来自文件:

Java多线程编程核心技术_完整版 PDF电子书下载 带书签目录@www.java1234.com.pdf

下载链接:

链接:https://pan.baidu.com/s/1014DK3LnjRRpPOSFjS7yIg 
提取码:bnan 
复制这段内容后打开百度网盘手机App,操作更方便哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱的叹息

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值