java多线程的三种创建方式
第一种方式,继承Thread类,重写run方法,这里我模拟了一个买票的系统,四个窗口同时开始卖票
/**
* 第一种方式,继承Thread类,重写run方法
*
* @author admin
*
*/
public class MyThread extends Thread {
private static int num = 100; // 共享变量(基本数据类型的数据),不能当对象锁---因为它不是对象
private static Object obj = new Object();// 可以新建一个与基本数据类型的共享变量平行的对象,来替代它当作锁
private String name = null;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
while (true) {
synchronized (obj) {// ※静态变量的对象锁是不能用this来代替的
if (num > 0) {
System.out.println(name + ":" + num);
num--;//卖出去了一张,减去一张
try {
Thread.sleep(50);//睡眠0.02秒
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
}
}
}
第一种方法测试类
/**
* 测试第一种方式
*
* @author admin
*
*/
public class MyThreadTest {
public static void main(String[] args) {
// 开四个窗口(new线程),让它们都开始卖票(启动线程)
Thread t1 = new MyThread("窗口1");
Thread t2 = new MyThread("窗口2");
Thread t3 = new MyThread("窗口3");
Thread t4 = new MyThread("窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
控制台输出
窗口1:100
窗口1:99
窗口1:98
窗口1:97
窗口1:96
窗口1:95
窗口1:94
窗口1:93
窗口1:92
窗口1:91
窗口1:90
窗口1:89
窗口1:88
窗口1:87
窗口1:86
窗口1:85
窗口1:84
窗口1:83
窗口1:82
窗口1:81
窗口1:80
窗口1:79
窗口1:78
窗口1:77
窗口1:76
窗口1:75
窗口1:74
窗口1:73
窗口1:72
窗口1:71
窗口1:70
窗口1:69
窗口1:68
窗口1:67
窗口1:66
窗口1:65
窗口1:64
窗口1:63
窗口1:62
窗口1:61
窗口1:60
窗口1:59
窗口1:58
窗口1:57
窗口1:56
窗口1:55
窗口1:54
窗口1:53
窗口1:52
窗口1:51
窗口1:50
窗口1:49
窗口1:48
窗口1:47
窗口1:46
窗口1:45
窗口1:44
窗口1:43
窗口1:42
窗口4:41
窗口4:40
窗口4:39
窗口4:38
窗口4:37
窗口4:36
窗口4:35
窗口4:34
窗口4:33
窗口4:32
窗口4:31
窗口4:30
窗口4:29
窗口4:28
窗口4:27
窗口4:26
窗口4:25
窗口2:24
窗口2:23
窗口2:22
窗口2:21
窗口2:20
窗口2:19
窗口2:18
窗口2:17
窗口2:16
窗口2:15
窗口2:14
窗口2:13
窗口2:12
窗口2:11
窗口2:10
窗口2:9
窗口2:8
窗口2:7
窗口2:6
窗口2:5
窗口2:4
窗口2:3
窗口2:2
窗口2:1
第二种方式,实现runnable接口,重写run方法。这种方法跟第一种类似,因为Thread类实现的就是runnable接口
/**
* 第二种方式,实现runnable接口,重写run方法
*
* @author admin
*
*/
public class MyRunnable implements Runnable {
private static int num = 100;// 共享变量(基本数据类型的数据),不能当对象锁---因为它不是对象
private static Object obj = new Object();// 可以新建一个与基本数据类型的共享变量平行的对象,来替代它当作锁
@Override
public void run() {
while (num > 0) {
synchronized (obj) {
if (num > 0) {
System.out.println(Thread.currentThread().getName() + "---" + num);
num--;//卖出去了一张,减去一张
try {
Thread.sleep(50);//睡眠0.05秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
第二种方式测试
/**
* 测试第二种方式
*
* @author admin
*
*/
public class MyRunnableTest {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();//创建runnable对象
Thread thread1 = new Thread(myRunnable);//runnable对象作为Thread对象的target创建新的线程
Thread thread2 = new Thread(myRunnable);
Thread thread3 = new Thread(myRunnable);
thread1.start();
thread2.start();
thread3.start();
}
}
控制台
Thread-0---100
Thread-0---99
Thread-0---98
Thread-0---97
Thread-0---96
Thread-0---95
Thread-0---94
Thread-0---93
Thread-0---92
Thread-0---91
Thread-0---90
Thread-0---89
Thread-0---88
Thread-0---87
Thread-1---86
Thread-1---85
Thread-1---84
Thread-1---83
Thread-1---82
Thread-1---81
Thread-1---80
Thread-1---79
Thread-1---78
Thread-1---77
Thread-1---76
Thread-1---75
Thread-1---74
Thread-1---73
Thread-1---72
Thread-1---71
Thread-1---70
Thread-1---69
Thread-1---68
Thread-1---67
Thread-2---66
Thread-2---65
Thread-2---64
Thread-2---63
Thread-2---62
Thread-2---61
Thread-2---60
Thread-2---59
Thread-2---58
Thread-2---57
Thread-2---56
Thread-2---55
Thread-2---54
Thread-2---53
Thread-2---52
Thread-2---51
Thread-2---50
Thread-2---49
Thread-2---48
Thread-2---47
Thread-2---46
Thread-2---45
Thread-2---44
Thread-2---43
Thread-2---42
Thread-2---41
Thread-2---40
Thread-2---39
Thread-2---38
Thread-2---37
Thread-2---36
Thread-2---35
Thread-2---34
Thread-2---33
Thread-2---32
Thread-2---31
Thread-2---30
Thread-2---29
Thread-2---28
Thread-1---27
Thread-1---26
Thread-1---25
Thread-1---24
Thread-1---23
Thread-1---22
Thread-1---21
Thread-1---20
Thread-1---19
Thread-1---18
Thread-1---17
Thread-1---16
Thread-1---15
Thread-1---14
Thread-1---13
Thread-1---12
Thread-1---11
Thread-1---10
Thread-1---9
Thread-1---8
Thread-1---7
Thread-1---6
Thread-1---5
Thread-1---4
Thread-1---3
Thread-1---2
Thread-1---1
第三种方式,实现callable接口,重写call方法,这种方法可以获取线程的返回值
/**
* 第三种方式,实现callable接口,重写call方法
*
* @author admin
*
*/
public class MyCallable implements Callable<Integer> {
private static int i = 0;
@Override
public Integer call() throws Exception {
int sum = 0;
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
sum += i;
}
return sum;
}
}
第三种方式测试
/**
* 测试第三种方式
*
* @author admin
*
*/
public class MyCallableTest {
public static void main(String[] args) {
Callable<Integer> myCallable = new MyCallable(); // 创建MyCallable对象
FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); // 使用FutureTask来包装MyCallable对象
Thread thread = new Thread(ft); // FutureTask对象作为Thread对象的target创建新的线程
thread.start(); // 线程进入到就绪状态
try {
int sum = ft.get(); // 用FutureTask对象的get()方法取得新创建的新线程中的call()方法返回的结果
System.out.println("sum = " + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
控制台
Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-0 6
Thread-0 7
Thread-0 8
Thread-0 9
Thread-0 10
Thread-0 11
Thread-0 12
Thread-0 13
Thread-0 14
Thread-0 15
Thread-0 16
Thread-0 17
Thread-0 18
Thread-0 19
Thread-0 20
Thread-0 21
Thread-0 22
Thread-0 23
Thread-0 24
Thread-0 25
Thread-0 26
Thread-0 27
Thread-0 28
Thread-0 29
Thread-0 30
Thread-0 31
Thread-0 32
Thread-0 33
Thread-0 34
Thread-0 35
Thread-0 36
Thread-0 37
Thread-0 38
Thread-0 39
Thread-0 40
Thread-0 41
Thread-0 42
Thread-0 43
Thread-0 44
Thread-0 45
Thread-0 46
Thread-0 47
Thread-0 48
Thread-0 49
Thread-0 50
Thread-0 51
Thread-0 52
Thread-0 53
Thread-0 54
Thread-0 55
Thread-0 56
Thread-0 57
Thread-0 58
Thread-0 59
Thread-0 60
Thread-0 61
Thread-0 62
Thread-0 63
Thread-0 64
Thread-0 65
Thread-0 66
Thread-0 67
Thread-0 68
Thread-0 69
Thread-0 70
Thread-0 71
Thread-0 72
Thread-0 73
Thread-0 74
Thread-0 75
Thread-0 76
Thread-0 77
Thread-0 78
Thread-0 79
Thread-0 80
Thread-0 81
Thread-0 82
Thread-0 83
Thread-0 84
Thread-0 85
Thread-0 86
Thread-0 87
Thread-0 88
Thread-0 89
Thread-0 90
Thread-0 91
Thread-0 92
Thread-0 93
Thread-0 94
Thread-0 95
Thread-0 96
Thread-0 97
Thread-0 98
Thread-0 99
sum = 4950
个人写的小Demo,如有不足还请各路大神指教