多线程编程题目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26323323/article/details/83750198

1.现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?

// 创建线程对象,执行打印方法	
private static class ForthObject extends Thread{

	public ForthObject(String name){
        super(name);
    }
    @Override
    public void run(){
        for(int i=0;i<5;i++){
            System.out.println(this.getName() + ":" + i);
        }
    }
}

// 主线程中使用
public class Test {

	public static void main(String[] args) {
		
		// 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
		ForthObject p1 = new ForthObject("1");
		ForthObject p2 = new ForthObject("2");
		ForthObject p3 = new ForthObject("3");
		
		try {
			//
			p1.start();
			p1.join();
			p2.start();
			p2.join();
			p3.start();
			p3.join();
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

注意:需要先调用start方法后调用join方法;p1线程调用join方法后就会先执行p1线程的方法

结果如下:

1:0
1:1
1:2
1:3
1:4
2:0
2:1
2:2
2:3
2:4
3:0
3:1
3:2
3:3
3:4

2.两个线程轮流打印数字,一直到100

// 创建对象,
// 分别创建两个方法,我们使用flag来控制执行print1方法还是print2方法
private static class SecondObject{
	private static boolean flag = true;
	private int count = 0;
	
	public synchronized void print1(){
		for (int i = 1; i <= 50; i++) {
			if(!flag){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print(++count);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			flag = !flag;
			
			this.notifyAll();
		}
	}
	
	public synchronized void print2(){
		for (int i = 1; i <= 50; i++) {
			if(flag){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print(++count);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			flag = !flag;
			
			this.notifyAll();
		}
	}
}


public class Test {

	public static void main(String[] args) {
		
		
		// 2.两个线程轮流打印数字,一直到100
		SecondObject so = new SecondObject();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				so.print1();
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				so.print2();
			}
		}).start();

	}
}

结果如题目所要求,轮询打印

3.写两个线程,一个线程打印1~52,另一个线程打印A~Z,打印顺序是12A34B...5152Z

// 写一个类,分别打印字母和数字
// 两个方法通过flag来控制打印
private static class MyObject{
	private static boolean flag = true;
	private int count = 0;
	// 写数字
	public synchronized void printNum(){
	
		for (int i = 0; i < 52; i++) {
			
			// 默认为true,所以让其先打印
			if(!flag){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print(++count);
			System.out.print(++count);
			
			// 唤醒printChar
			flag = !flag;
			this.notifyAll();
		}
	}
	
	// 写字母
	public synchronized void printChar(){
		
		for (int i = 0; i < 26; i++) {
			
			// 默认为true,所以让其后打印
			if(flag){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print((char)(65+i));
			
			// 唤醒printNum
			flag = !flag;
			this.notifyAll();
		}
		
	}
}


// 执行main方法
public class Test {

	public static void main(String[] args) {
		
		// 1.写两个线程,一个线程打印1~52,另一个线程打印A~Z,打印顺序是12A34B...5152Z
		MyObject m = new MyObject();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				m.printNum();
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				m.printChar();
			}
		}).start();
    }
}

结果如题目所要求

4.编写一个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC...

// 创建对象,分别打印A B C
// 我们通过flag来控制执行哪个方法
private static class ThirdObject{
	
	private volatile int flag = 1;
	
	public synchronized void printA(){
		for (int i = 0; i < 5; i++) {
			
			// 如果不为1 ,说明还没有轮到当前线程执行方法,则进入wait方法,释放对象锁
			if(flag != 1){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print("A ");
			
			// 下一个应该printB方法来执行
			flag = 2;
			this.notifyAll();
		}
	}
	
	public synchronized void printB(){
		for (int i = 0; i < 5; i++) {
			
			// 如果不为2 ,说明还没有轮到当前线程执行方法,则进入wait方法,释放对象锁
			if(flag != 2){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print("B ");
			
			// 下一个应该printC方法来执行
			flag = 3;
			this.notifyAll();
		}
	}
	
	public synchronized void printC(){
		for (int i = 0; i < 5; i++) {
			
			// 如果不为3 ,说明还没有轮到当前线程执行方法,则进入wait方法,释放对象锁
			if(flag != 3){
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			System.out.print("C ");
			
			// 下一个应该printA方法来执行
			flag = 1;
			this.notifyAll();
		}
	}
}

// 执行main方法
public class Test {

	public static void main(String[] args) {
		
		
		// 3.编写一个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC...
		ThirdObject to = new ThirdObject();
		new Thread(new Runnable() {
					
			@Override
			public void run() {
				to.printA();
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				to.printB();
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				to.printC();
			}
		}).start();
    }
}

结果如题目所要求

猜你喜欢

转载自blog.csdn.net/qq_26323323/article/details/83750198