多线程题目

实现在一个程序中同时完成如下两个任务,任务一:能将ASCII值为1到100对应的字符输出到控制台;任务二:能将1-100的数以数输出。要求他们交叉输出。

方法一

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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Test1 class
*
* @author 蒋时华
* @date 2019/10/23
*/
public class Test {

public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(2);

Test test = new Test();


executorService.execute(()->{
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
test.ascll();
});
executorService.execute(()->{
test.number();
});
}

public synchronized void ascll(){
for (int i = 1; i < 100; i++) {
System.out.println((char)i);
notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public synchronized void number(){
for (int i = 1; i < 100; i++) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
notify();
}
}

}
方法二

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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Test1 class
*
* @author 蒋时华
* @date 2019/10/23
*/
public class Test {

volatile boolean lock = false;

public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(100);

Test test = new Test();
executorService.execute(()->{
test.ascll();
});
executorService.execute(()->{
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
test.number();
});

}

public void ascll(){
for (int i = 1; i < 100; i++) {
while (lock){

}
System.out.println((char)i);
lock = true;
}
}

public void number(){
for (int i = 1; i < 100; i++) {
while (!lock){

}
System.out.println(i);
lock = false;
}
}

}
方法三

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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
* Test1 class
*
* @author 蒋时华
* @date 2019/10/23
*/
public class Test {

ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();

public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(100);

Test test = new Test();
executorService.execute(()->{
test.ascll();
});
executorService.execute(()->{
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
test.number();
});

}

public void ascll(){
for (int i = 1; i < 100; i++) {
try{
lock.lock();
System.out.println((char)i);
condition.signal();
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}

public void number(){
for (int i = 1; i < 100; i++) {
try{
lock.lock();
System.out.println(i);
condition.signal();
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}

}
方法四

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
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Test1 class
*
* @author 蒋时华
* @date 2019/10/23
*/
public class Test {

// CountDownLatch countDownLatch = new CountDownLatch(1);
CyclicBarrier cyclicBarrier = new CyclicBarrier(2);


public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(100);
Test test = new Test();
executorService.execute(()->{
test.ascll();
});
executorService.execute(()->{
test.number();
});
}

public void ascll(){
for (int i = 1; i < 100; i++) {
System.out.println((char)i);
try {
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
try {
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void number(){
for (int i = 1; i < 100; i++) {
try {
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(i);
try {
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
方法五

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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Test1 class
*
* @author 蒋时华
* @date 2019/10/23
*/
public class Test {

Semaphore semaphore = new Semaphore(1, true);

public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(100);
Test test = new Test();
executorService.execute(()->{
test.ascll();
});
executorService.execute(()->{
test.number();
});
}

public void ascll(){
// 保证另一个线程启动
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 1; i < 100; i++) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println((char)i);

semaphore.release();
}
}

public void number(){
// 保证另一个线程启动
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 1; i < 100; i++) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
semaphore.release();
}
}


}

下一题