1. 开启线程

继承实现

代码

package cn.edut.com.tarena;

import org.junit.Test;

public class Demo01_Thread {
	@Test
	public void test001() {
		int time= 1000;
		T1 t1 = new T1(time);
		T1 t2 = new T1(time);
		//线程启动后,会自动执行run()方法代码
		t1.start();
		t2.start();
		
	}
}


class T1 extends Thread{
	private int time  ; 
	public T1(int time) {
		this.time = time;
	}
	@Override
	public void run() {
		for(int i=0 ; i<time ; i++) {
			System.out.println(this.getName() +" - "+ i);
			
		}
		super.run();
	}
}


接口实现

代码

package cn.edut.com.tarena;

import org.junit.Test;

import com.sun.xml.internal.ws.api.server.ThreadLocalContainerResolver;

public class Demo02_Thread_Interface {
	/** * 共用一个类 */
	@Test
	public void test001() {
		System.out.println("test1:");
		T2 t2 = new T2(100);
		Thread thread1 = new Thread(t2);
		Thread thread2 = new Thread(t2);
		thread1.start();
		thread2.start();
	}
	/** * 两个类 */
	@Test
	public void test002() {
		System.out.println("test2:");
		int time = 100 ;
		new Thread(new T2(time)).start(); 
		new Thread(new T2(time)).start(); 
	}
}

class T2 implements Runnable{
	private int i=0  ; 
	private int time; 
	public  T2(int t) {
		time = t;
	}
	
	@Override
	public void run() {
		for( ;i<time;i++ ) {
			System.out.println(Thread.currentThread().getName()+" - "+i);
		}
	}
	
}



结果 - test1

两个线程共用同一个类

test1:
Thread-1 - 0
Thread-1 - 1
Thread-1 - 2
Thread-1 - 3
Thread-1 - 4
Thread-1 - 5
Thread-1 - 6
Thread-1 - 7
Thread-1 - 8
Thread-1 - 9
Thread-1 - 10
Thread-1 - 11
Thread-1 - 12
Thread-1 - 13
Thread-1 - 14
Thread-1 - 15
Thread-1 - 16
Thread-1 - 17
Thread-1 - 18
Thread-1 - 19
Thread-1 - 20
Thread-1 - 21
Thread-1 - 22
Thread-1 - 23
Thread-1 - 24
Thread-1 - 25
Thread-1 - 26
Thread-1 - 27
Thread-1 - 28
Thread-1 - 29
Thread-1 - 30
Thread-1 - 31
Thread-1 - 32
Thread-1 - 33
Thread-1 - 34
Thread-1 - 35
Thread-1 - 36
Thread-1 - 37
Thread-1 - 38
Thread-1 - 39
Thread-1 - 40
Thread-1 - 41
Thread-1 - 42
Thread-1 - 43
Thread-1 - 44
Thread-1 - 45
Thread-1 - 46
Thread-1 - 47
Thread-1 - 48
Thread-1 - 49
Thread-1 - 50
Thread-1 - 51
Thread-1 - 52
Thread-1 - 53
Thread-1 - 54
Thread-1 - 55
Thread-1 - 56
Thread-1 - 57
Thread-1 - 58
Thread-1 - 59
Thread-1 - 60
Thread-1 - 61
Thread-1 - 62
Thread-1 - 63
Thread-1 - 64
Thread-1 - 65
Thread-1 - 66
Thread-1 - 67
Thread-1 - 68
Thread-1 - 69
Thread-1 - 70
Thread-1 - 71
Thread-1 - 72
Thread-1 - 73
Thread-1 - 74
Thread-1 - 75
Thread-1 - 76
Thread-1 - 77
Thread-1 - 78
Thread-1 - 79
Thread-1 - 80
Thread-1 - 81
Thread-1 - 82
Thread-1 - 83
Thread-1 - 84
Thread-1 - 85
Thread-1 - 86
Thread-1 - 87
Thread-1 - 88
Thread-1 - 89
Thread-1 - 90
Thread-1 - 91
Thread-1 - 92
Thread-1 - 93
Thread-1 - 94
Thread-1 - 95
Thread-1 - 96
Thread-1 - 97
Thread-1 - 98
Thread-1 - 99



结果 - test2

两个线程没有关系

test2:
Thread-1 - 0
Thread-1 - 1
Thread-1 - 2
Thread-1 - 3
Thread-1 - 4
Thread-1 - 5
Thread-1 - 6
Thread-1 - 7
Thread-1 - 8
Thread-1 - 9
Thread-1 - 10
Thread-1 - 11
Thread-1 - 12
Thread-1 - 13
Thread-1 - 14
Thread-1 - 15
Thread-1 - 16
Thread-1 - 17
Thread-1 - 18
Thread-1 - 19
Thread-1 - 20
Thread-1 - 21
Thread-1 - 22
Thread-1 - 23
Thread-1 - 24
Thread-1 - 25
Thread-1 - 26
Thread-1 - 27
Thread-1 - 28
Thread-1 - 29
Thread-1 - 30
Thread-1 - 31
Thread-1 - 32
Thread-1 - 33
Thread-1 - 34
Thread-1 - 35
Thread-1 - 36
Thread-1 - 37
Thread-1 - 38
Thread-1 - 39
Thread-1 - 40
Thread-1 - 41
Thread-1 - 42
Thread-1 - 43
Thread-1 - 44
Thread-1 - 45
Thread-1 - 46
Thread-1 - 47
Thread-1 - 48
Thread-1 - 49
Thread-1 - 50
Thread-1 - 51
Thread-1 - 52
Thread-1 - 53
Thread-1 - 54
Thread-1 - 55
Thread-1 - 56
Thread-1 - 57
Thread-1 - 58
Thread-1 - 59
Thread-1 - 60
Thread-1 - 61
Thread-1 - 62
Thread-1 - 63
Thread-1 - 64
Thread-1 - 65
Thread-1 - 66
Thread-1 - 67
Thread-1 - 68
Thread-1 - 69
Thread-1 - 70
Thread-1 - 71
Thread-1 - 72
Thread-1 - 73
Thread-1 - 74
Thread-1 - 75
Thread-1 - 76
Thread-1 - 77
Thread-1 - 78
Thread-1 - 79
Thread-1 - 80
Thread-1 - 81
Thread-1 - 82
Thread-1 - 83
Thread-1 - 84
Thread-1 - 85
Thread-1 - 86
Thread-1 - 87
Thread-1 - 88
Thread-1 - 89
Thread-1 - 90
Thread-1 - 91
Thread-1 - 92
Thread-1 - 93
Thread-1 - 94
Thread-1 - 95
Thread-1 - 96
Thread-1 - 97
Thread-1 - 98
Thread-1 - 99
Thread-2 - 0
Thread-2 - 1
Thread-2 - 2
Thread-2 - 3
Thread-2 - 4
Thread-2 - 5
Thread-2 - 6
Thread-2 - 7
Thread-2 - 8
Thread-2 - 9
Thread-2 - 10
Thread-2 - 11
Thread-2 - 12
Thread-2 - 13
Thread-2 - 14
Thread-2 - 15
Thread-2 - 16
Thread-2 - 17
Thread-2 - 18
Thread-2 - 19
Thread-2 - 20
Thread-2 - 21
Thread-2 - 22
Thread-2 - 23
Thread-2 - 24
Thread-2 - 25
Thread-2 - 26
Thread-2 - 27
Thread-2 - 28
Thread-2 - 29
Thread-2 - 30
Thread-2 - 31
Thread-2 - 32
Thread-2 - 33
Thread-2 - 34
Thread-2 - 35
Thread-2 - 36
Thread-2 - 37
Thread-2 - 38
Thread-2 - 39
Thread-2 - 40
Thread-2 - 41
Thread-2 - 42
Thread-2 - 43
Thread-2 - 44
Thread-2 - 45
Thread-2 - 46
Thread-2 - 47
Thread-2 - 48
Thread-2 - 49
Thread-2 - 50
Thread-2 - 51
Thread-2 - 52
Thread-2 - 53
Thread-2 - 54
Thread-2 - 55
Thread-2 - 56
Thread-2 - 57
Thread-2 - 58


2. Thread方法测试

sleep() - 打印当前时间

代码

package cn.edut.com.tarena;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo03_Thread_Method {
	public static void main(String[] args) {
		new T1().start();;
	}
	
	static class T1 extends Thread{
		@Override
		public void run() {
			SimpleDateFormat SDF = new SimpleDateFormat("hh:MM:ss");
			while (true) {
				Date d = new Date();
				System.out.println(SDF.format(d));
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
}



结果



interrupt() - 回车中断打印时间

代码

package cn.edut.com.tarena;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Demo03_Thread_Method {
	public static void main(String[] args) {
		T1 thread1 = new T1();
		thread1.start();

		new Thread() {
			@Override
			public void run() {
				Scanner sc = new Scanner(System.in);
				while (true) {
					sc.nextLine();
					thread1.interrupt();
				}

			}
		}.start();
	}

	static class T1 extends Thread {
		@Override
		public void run() {
			SimpleDateFormat SDF = new SimpleDateFormat("hh:MM:ss");
			while (true) {
				System.out.println(SDF.format(new Date()));
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					System.out.println("中断");
					break;
				}
			}
		}
	}

}



结果




setDaemon(boolean) - 守护线程

当前面的线程结束后,守护线程也会结束。

代码

package cn.edut.com.tarena;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Demo03_Thread_Method {
	public static void main(String[] args) {
		T1 thread1 = new T1();
		thread1.start();

		Thread thread2 = new Thread() {
			@Override
			public void run() {
				Scanner sc = new Scanner(System.in);
				while (true) {
					sc.nextLine();
					thread1.interrupt();
				}

			}
		};
		//↓↓↓↓↓↓↓↓↓↓
		thread2.setDaemon(true);
		//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
		thread2.start();
		
		
	}

	static class T1 extends Thread {
		@Override
		public void run() {
			SimpleDateFormat SDF = new SimpleDateFormat("hh:MM:ss");
			while (true) {
				System.out.println(SDF.format(new Date()));
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					System.out.println("中断");
					break;
				}
			}
		}
	}

}

结果

按了回车之后,计数就结束



join() - 素数计数、单线程多线程时间对比

判断素数的程序:
https://blog.csdn.net/LawssssCat/article/details/102921354

代码

package cn.edut.com.tarena;

import org.junit.Test;

public class Demo4_Thread_join {
	class T1 extends Thread{
		public T1(long start , long end) {
			this.start = start;
			this.end = end ;
		}
		
		private long start ; 
		private long end; 
		private long count ; 
		
		@Override
		public void run() {
			for(long i=start ; i<end; i++ ) {
				if(isPrime(i)) {
					count++ ; 
				}
			}
		}
	}
	

	public boolean isPrime(long num) {
		if (num < 2) {
			return false;
		} else if (num > 2) { // 偶数
			if (num % 2 == 0) {
				return false;
			} else {
				int len = (int) Math.sqrt(num);
				for (long i = 2; i <= len; i++) {
					if (num % i == 0) {
						return false ;
					}
				}
			}
		}
		return true;
	}
	

	/** * 计算素数 定义::质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。 2、3、5、7、11 ... * 判断素数 */
	@Test
	
	public void test001() {
		System.out.println("test01测试:(函数判断素数)");
		for (int i = 0; i < 100; i++) {
			System.out.println(i + " : " + isPrime(i));
		}
	}
		 
	@Test
	public void test002() throws InterruptedException {
		System.out.println("test02测试:(单线程、多线程、时间对比)");
		//从0计算到n
		int n = 400000;
		//单线程
		long t1 = System.currentTimeMillis();
		T1 thread0 = new T1(0, n); 
		thread0.start();  
		thread0.join();
		System.out.println("单线程计数"+thread0.count);
		long t2 = System.currentTimeMillis();
		int tn = 5 ; 
		T1[] ts = new T1[tn] ;
		for(int i=0 ; i<tn ; i++) {
			ts[i] = new T1(i*n/tn, (i+1)*n/tn);
			ts[i].start();
		}
		int count =0 ;
		for(T1 t : ts) {
			t.join();
			count+=t.count;
		}
		System.out.println("多线程计数:"+count);
		long t3 = System.currentTimeMillis();
		System.out.println("单线程运行时间:"+(t2-t1)+"(毫秒)");
		System.out.println("多线程运行时间:"+(t3-t2)+"(毫秒)");
		
		
	}

	
	
}



结果

test01测试:(函数判断素数)
0 : false
1 : false
2 : true
3 : true
4 : false
5 : true
6 : false
7 : true
8 : false
9 : false
10 : false
11 : true
12 : false
13 : true
14 : false
15 : false
16 : false
17 : true
18 : false
19 : true
20 : false
21 : false
22 : false
23 : true
24 : false
25 : false
26 : false
27 : false
28 : false
29 : true
30 : false
31 : true
32 : false
33 : false
34 : false
35 : false
36 : false
37 : true
38 : false
39 : false
40 : false
41 : true
42 : false
43 : true
44 : false
45 : false
46 : false
47 : true
48 : false
49 : false
50 : false
51 : false
52 : false
53 : true
54 : false
55 : false
56 : false
57 : false
58 : false
59 : true
60 : false
61 : true
62 : false
63 : false
64 : false
65 : false
66 : false
67 : true
68 : false
69 : false
70 : false
71 : true
72 : false
73 : true
74 : false
75 : false
76 : false
77 : false
78 : false
79 : true
80 : false
81 : false
82 : false
83 : true
84 : false
85 : false
86 : false
87 : false
88 : false
89 : true
90 : false
91 : false
92 : false
93 : false
94 : false
95 : false
96 : false
97 : true
98 : false
99 : false
test02测试:(单线程、多线程、时间对比)
单线程计数33860
多线程计数:33860
单线程运行时间:257(毫秒)
多线程运行时间:126(毫秒)