并发练习题02
2019-07-09 本文已影响0人
流光已至
1 写一个程序,线程C在线程B后执行,线程B在线程A之后进行
public static void main(String[] args) {
Thread aThread = new Thread(){
@Override
public void run() {
System.out.println("can I do A........");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("This is A");
}
};
Thread bThread = new Thread(){
@Override
public void run() {
System.out.println("can I do B........");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("This is B");
}
};
Thread cThread = new Thread(){
@Override
public void run() {
System.out.println("can I do C........");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("This is C");
}
};
try {
aThread.start();
aThread.join();
bThread.start();
bThread.join();
cThread.start();
cThread.join();
} catch (Exception e) {
// TODO: handle exception
}
}
2 编写4个线程分别完成创建F1.txt,F2.txt,F3.txt,F4.txt的文件,并分别重复4次写入"ABCD","BCDA","CDBA","DABC"到各自对应的文件中。
// 注意抽取封装,不要总是main函数
static class FileRunnable implements Runnable{
private String pathName;
private String Context;
public FileRunnable(String pathName, String context) {
super();
this.pathName = pathName;
Context = context;
}
@Override
public void run() {
File file = new File(pathName);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
for(int i=0;i<4;i++){
bw.write(Context);
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Thread thread1=new Thread(new FileRunnable("E:\\F1.txt","ABCD"));
thread1.start();
Thread thread2=new Thread(new FileRunnable("E:\\F2.txt","BCDA"));
thread2.start();
Thread thread3=new Thread(new FileRunnable("E:\\F3.txt","CDAB"));
thread3.start();
Thread thread4=new Thread(new FileRunnable("E:\\F4.txt","DABC"));
thread4.start();
}
3 P03 有三个车库,模拟多个用户停车、离开的效果
public class P03 {
/*有三个车库,模拟多个用户停车、离开的效果*/
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private BlockingQueue<Thread> blockingQueue =new ArrayBlockingQueue<>(3);
public void parking() throws InterruptedException {
try {
lock.lock();
while (blockingQueue.size()==3) {
condition.await();
}
blockingQueue.add(Thread.currentThread());
System.out.println("第" + Thread.currentThread().getName() + "号车停车了");
System.out.println("已停有"+blockingQueue.size()+"辆车");
condition.signal();
} finally {
lock.unlock();
}
}
public void outing() throws InterruptedException {
try {
lock.lock();
while (blockingQueue.isEmpty()) {
condition.await();
}
Thread peek = blockingQueue.remove();
System.out.println("第" + peek.getName() + "号车离开了");
System.out.println("已停有"+blockingQueue.size()+"辆车");
condition.signal();
} finally {
lock.unlock();
}
}
}
class park implements Runnable{
private P03 p03;
public park(P03 p03) {
super();
this.p03 = p03;
}
@Override
public void run() {
try {
p03.parking();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class out implements Runnable{
private P03 p03;
public out(P03 p03) {
super();
this.p03 = p03;
}
@Override
public void run() {
try {
Thread.sleep(100);
p03.outing();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
4 创建两个线程,其中一个输出1-52,另外一个输出A-Z。输出格式要求:12A 34B 56C 78D*/
/*创建两个线程,其中一个输出1-52,另外一个输出A-Z。输出格式要求:12A 34B 56C 78D*/
public static void po1(){
Object lock = new Object();
Thread t1 = new Thread(new out1(lock));
Thread t2 = new Thread(new out2(lock));
t1.start();
t2.start();
}
public static void main(String[] args) {
po1();
}
}
class out1 implements Runnable{
private Object lock;
public out1(Object lock) {
this.lock = lock;
}
@Override
public void run() {
for (int i = 1; i < 52; i += 2) {
// 对Lock对象上锁
synchronized (lock) {
System.out.print(i);
System.out.print(i + 1);
try {
// 先释放等待的线程 再进入等待状态
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class out2 implements Runnable{
private Object lock;
public out2(Object lock) {
this.lock = lock;
}
@Override
public void run() {
int A= 'A';
for (int i=0;i<26;i++) {
synchronized (lock) {
System.out.println((char)(A++));
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}