线程按顺序执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @Author lj
* @Date 2018/6/11 20:18
*/
public class ThreadTest {

public static class Thread1 extends Thread{
@Override
public void run() {
System.out.println(1);
}
}

public static class Thread2 extends Thread{
@Override
public void run() {
System.out.println(2);
}
}

public static class Thread3 extends Thread{
@Override
public void run() {
System.out.println(3);
}
}

public static void main(String[] args) throws Exception{
ThreadSx();
ThreadSx2();
}

private static void ThreadSx() {
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(()->System.out.println(1));
service.submit(()->System.out.println(2));
service.submit(()->System.out.println(3));
service.shutdown();
}

private static void ThreadSx2() throws Exception{
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
Thread3 t3 = new Thread3();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}

线程按顺序循环执行

synchronized

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @Author lj
* @Date 2018/6/12 11:04
*/
public class ThreadTest2 {

private static volatile int s = 1;

public static class Thread1 extends Thread {
@Override
public void run() {
synchronized (ThreadTest2.class) {
for (int i = 0; i < 10;) {
if (s == 1) {
System.out.println(1);
s = 2;
i++;
ThreadTest2.class.notifyAll();
} else {
try {
ThreadTest2.class.wait();
} catch (Exception e) {

}
}
}
}
}
}

public static class Thread2 extends Thread {
@Override
public void run() {
synchronized (ThreadTest2.class) {
for (int i = 0; i < 10;) {

if (s == 2) {
System.out.println(2);
s = 3;
i++;
ThreadTest2.class.notifyAll();
} else {
try {
ThreadTest2.class.wait();
} catch (Exception e) {

}
}

}
}
}
}

public static class Thread3 extends Thread {
@Override
public void run() {
synchronized (ThreadTest2.class) {
for (int i = 0; i < 11;) {
if (s == 3) {
System.out.println(3);
s = 1;
i++;
ThreadTest2.class.notifyAll();
} else {
try {
ThreadTest2.class.wait();
} catch (Exception e) {
}
}
}
}
}
}

public static void main(String[] args)throws Exception {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
Thread3 t3 = new Thread3();
t1.start();
t2.start();
t3.start();
}
}

ReentrantLock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @Author lj
* @Date 2018/6/12 13:44
*/
public class ThreadTest3 {

private static ReentrantLock lock = new ReentrantLock();

private static volatile int s = 1;

public static class Thread1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; ) {
lock.lock();
if (s == 1) {
System.out.println(1);
s = 2;
i++;
}
lock.unlock();
}

}
}
public static class Thread2 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10;) {
lock.lock();
if (s == 2) {
System.out.println(2);
s = 3;
i++;
}
lock.unlock();
}

}
}
public static class Thread3 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; ) {
lock.lock();
if (s == 3) {
System.out.println(3);
s = 1;
i++;
}
lock.unlock();
}

}
}
public static void main(String[] args)throws Exception {
new Thread1().start();
new Thread2().start();
new Thread3().start();
}
}

Condition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @Author lj
* @Date 2018/6/12 14:10
*/
public class ThreadTest4 {

private static volatile int s = 1;

private static ReentrantLock lock = new ReentrantLock();

private static Condition condition1 = lock.newCondition();
private static Condition condition2 = lock.newCondition();
private static Condition condition3 = lock.newCondition();

private static class Thread1 extends Thread {
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 10; ) {
if (s != 1) {
condition1.await();
} else {
System.out.println(1);
s=2;
i++;
condition2.signal();
}
}
} catch (Exception e) {

} finally {
lock.unlock();
}
}
}

private static class Thread2 extends Thread {
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 10; ) {
if (s != 2) {
condition2.await();
} else {
System.out.println(2);
s=3;
i++;
condition3.signal();
}
}
} catch (Exception e) {

} finally {
lock.unlock();
}
}
}

private static class Thread3 extends Thread {
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 10; ) {
if (s != 3) {
condition3.await();
} else {
System.out.println(3);
s=1;
i++;
condition1.signal();
}
}
} catch (Exception e) {

} finally {
lock.unlock();
}
}
}

public static void main(String[] args) {
new Thread1().start();
new Thread2().start();
new Thread3().start();
}
}

Semaphore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @Author lj
* @Date 2018/6/12 14:26
*/
public class ThreadTest5 {

private static Semaphore semaphore1 = new Semaphore(1);
private static Semaphore semaphore2 = new Semaphore(1);
private static Semaphore semaphore3 = new Semaphore(1);

private static class Thread1 extends Thread{
@Override
public void run() {
try {
for(int i=0;i<10;i++){
semaphore1.acquire();
System.out.println(1);
semaphore2.release();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
private static class Thread2 extends Thread{
@Override
public void run() {
try {
for(int i=0;i<10;i++){
semaphore2.acquire();
System.out.println(2);
semaphore3.release();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
private static class Thread3 extends Thread{
@Override
public void run() {
try {
for(int i=0;i<10;i++){
semaphore3.acquire();
System.out.println(3);
semaphore1.release();
}
}catch (Exception e){
e.printStackTrace();
}
}
}

public static void main(String[] args) throws Exception {
semaphore2.acquire();
semaphore3.acquire();
new Thread1().start();
new Thread2().start();
new Thread3().start();
}
}