大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说多线程练习题,希望您对编程的造诣更进一步.
- 熟练掌握使用同步synchronized(){},wait(),notify(),notifyAll()解决多线程问题
- 熟练掌握使用lock锁机制解决多线程问题
- 熟练掌握使用阻塞队列BlockingQueue来解决多线程问题
题目一:假设车库有3个车位(可以通过boolean[]数组来表示车库)可以停车,写一个程序模拟多个用户开车离开,停车入库的效果。注意:车位有车时不能停车
一:使用synchronized(){}代码块来完成
package 多线程停车场;
public class Park {
private static Object obj = new Object();
private static int state = 3;//车位剩余数
public static void main(String[] args) {
//定义停车线程
Thread A = new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj) {
try {
while (true) {
while (state == 0) {
System.out.println("目前车位已满,请等候");
obj.wait();
}
state--;
System.out.println("停车成功,目前剩余车位位:" + state);
obj.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
//定义出车线程
Thread B = new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj) {
try {
while (true) {
Thread.sleep(1000);//模拟耗时操作
while (state == 3) {
obj.wait();
}
state++;
System.out.println("车辆出车,目前剩余车位:" + state);
obj.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
//开启线程
A.start();
B.start();
}
}
二:使用阻塞队列BlockingQueue来完成
入库操作
package 多线程停车场;
import java.util.concurrent.BlockingQueue;
/* *定义停车操作 */
public class Producter implements Runnable {
private BlockingQueue<Boolean> bq;
Producter(BlockingQueue<Boolean> bq) {
super();
this.bq = bq;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(1000);//模拟耗时操作
bq.put(true);
System.out.println("车辆停车,停车位剩余:"+(3-bq.size()));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
出库操作
package 多线程停车场;
import java.util.concurrent.BlockingQueue;
public class Consumer implements Runnable {
private BlockingQueue<Boolean> bq;
Consumer(BlockingQueue<Boolean> bq) {
this.bq = bq;
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(2000);//模拟耗时操作
bq.take();
System.out.println("车辆驶出,停车位剩余:"+(3-bq.size()));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试类:
package 多线程停车场;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class test {
public static void main(String[] args) {
BlockingQueue<Boolean> bq = new ArrayBlockingQueue<Boolean>(3);
Producter p1 = new Producter(bq);
Consumer c1 = new Consumer(bq);
Consumer c2 = new Consumer(bq);
//开启线程
new Thread(p1).start();
new Thread(c1).start();
new Thread(c2).start();
}
}
题目二:有A,B,C三个线程, A线程输出A, B线程输出B, C线程输出C,要求, 同时启动三个线程, 按顺序输出ABC, 循环10次。
一:使用lock锁
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ABC {
private static int state = 0;
public static void main(String[] args) {
final Lock l = new ReentrantLock();
//定义A线程
Thread A = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; ) {
l.lock();
if (state % 3 == 0) {
System.out.println("A");
state++;
i++;
}
l.unlock();
}
}
});
//定义B线程
Thread B = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; ) {
l.lock();
if (state % 3 == 1) {
System.out.println("B");
state++;
i++;
}
l.unlock();
}
}
});
//定义C线程
Thread C = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; ) {
l.lock();
if (state % 3 == 2) {
System.out.println("C");
state++;
i++;
}
l.unlock();
}
}
});
//开启三个线程
A.start();
B.start();
C.start();
}
}
二:使用同步代码块
/* 使用同步代码块与wait(),notifyAll()方法来实现 */
public class ABC_2 {
private static Object lock = new Object();
private static char currentChar = 'A';
public static void main(String[] args) {
//定义A线程
Thread A = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 10; ) {
try {
while (currentChar != 'A') {
lock.wait();
}
System.out.println("A");
currentChar = 'B';
i++;
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
lock.notifyAll();
}
}
}
}
});
//定义B线程
Thread B = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 10; ) {
try {
while (currentChar != 'B') {
lock.wait();
}
System.out.println("B");
currentChar = 'C';
i++;
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
lock.notifyAll();
}
}
}
}
});
//定义c线程
Thread C = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 10; ) {
try {
while (currentChar != 'C') {
lock.wait();
}
System.out.println("C");
currentChar = 'A';
i++;
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
lock.notifyAll();
}
}
}
}
});
//开启三个线程
A.start();B.start();C.start();
}
}
题目三: 编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。 即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束。
使用同步代码块实现:
public class Print {
private static int index = 0;
private static Object obj = new Object();
public static void main(String[] args) {
//定义打印数字线程
Thread A = new Thread(new Runnable() {
public void run() {
synchronized (obj) {
try {
for (int i = 1; i <= 52; i++) {
if (index % 3 == 2) {
obj.wait();
}
System.out.println(i);
index++;
obj.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
//定义打印字母线程
Thread B = new Thread(new Runnable() {
public void run() {
try {
synchronized (obj) {
for (char i = 'A'; i <= 'Z'; i++) {
while (index % 3 != 2) {
obj.wait();
}
System.out.println(i);
index++;
obj.notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
A.start();
B.start();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/13290.html