习题-多线程

目录

1.爬山

 2.线程优先级的设置

3.模拟医院叫号

4.模拟接力赛跑

5.网络购票


1.爬山

每个线程代表一个人,可设置每人爬山速度,每爬完100米显示信息,爬到终点时给出相应提示。

利用继承实现多线程

public class ClimbThread extends Thread{
	//爬100米的时长(线程休眠的时常)
	private int time;
	//爬多少个100米
	private int num;
	public ClimbThread(String name,int time,int kilo) {
		super(name);
		this.time = time;
		this.num = kilo*1000/100;
	}
	public void run() {
		//未爬到终点
		while (num > 0) {
			try {
				Thread.sleep(time);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"爬完了100米;");
			num--;
		}
		System.out.println(Thread.currentThread().getName()+"到达终点!");
	}
	
	
	
}
/*爬山
每个线程代表一个人
可设置每人爬山速度
每爬完100米显示信息
爬到终点时给出相应提示
*/
public class TestClimb {

	public static void main(String[] args) {
		ClimbThread cThread1 = new ClimbThread("年轻人", 150, 1);
		ClimbThread cThread2 = new ClimbThread("老年人", 350, 1);
		System.out.println("开始爬山");
		cThread1.start();
		cThread2.start();
	}

}

习题-多线程_第1张图片

------------------------------------------------------------------

 2.线程优先级的设置

将主线程设置为最高优先级、子线程设置为最低优先级并显示

子线程:

//显示主线程、子线程默认优先级
//将主线程设置为最高优先级、子线程设置为最低优先级并显示
public class MyThread implements Runnable{

	@Override
	public void run() {
	
	}

}
//获得和设置主线程及子线程的优先级
public class TestThread {

	public static void main(String[] args) {
		//获得主线程
		Thread mainThread = Thread.currentThread();
		
		//创建子线程
		Thread thread = new Thread(new MyThread());
		System.out.println("***********默认优先级*********");
		System.out.println("主线程名:"+mainThread.getName()+",优先级:"+mainThread.getPriority());
		System.out.println("子线程名:"+thread.getName()+",优先级:"+thread.getPriority());
		System.out.println("***********修改默认优先级后*********");
		mainThread.setPriority(10);
		thread.setPriority(1);
		System.out.println("主线程名:"+mainThread.getName()+",优先级:"+mainThread.getPriority());
		System.out.println("子线程名:"+thread.getName()+",优先级:"+thread.getPriority());
		
	}

}

习题-多线程_第2张图片

 ------------------------------------------------------------------

3.模拟医院叫号

n 某科室一天需看普通号2 0 个,特需号 10
n 特需号看病 时间是 普通号的 2
n 开始时普通号和特需号并行叫号 ,叫 到特需号的概率比普通号
n当普通号叫完第10号时,要求先看完全部特需号,再看普通
n 使用多线程模拟这一过程

特需号:

package dh07.hospital;
//特需号
public class SpecialThread implements Runnable{

	
	public void run() {
		for (int i = 1; i <= 10; i++) {
			System.out.println("特需号:"+i+"号病人在看病!");
			//线程休眠表示看病的过程
			try {
				Thread.sleep(400);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
	}
	
}
package dh07.hospital;

public class CommonThread {
	public static void main(String[] args) {
		

		
		Thread speThread = new Thread(new SpecialThread());
		speThread.setPriority(10);
		speThread.start();
		
		//模拟医院叫号
		for (int i = 1; i <= 20; i++) {
			System.out.println("普通号:"+i+"号病人再看病!");
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (i == 10) {
				try {
					speThread.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		}
		
	}
}

习题-多线程_第3张图片

 

 ------------------------------------------------------------------

4.模拟接力赛跑

多人参加1000米接力跑,每跑100米换人,每10米显示信息。

package dh07.run;
//创建线程类RunThread
public class RunThread implements Runnable{
	//多个选手共同跑1000米
	private int meters = 1000;
	//实现一个选手拿到接力棒跑100米的过程
	public void run() {
		while (true) {
			//同步代码块确保在跑步的只有一个人
			synchronized (this) {
				if (meters<100) {
					break;
				}
				System.out.println(Thread.currentThread().getName()+"拿到了接力棒!");
				for (int i = 10; i <= 100; i += 10) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+"跑了"+i+"米");
				}
				//剩余路途减少100米
				meters -= 100;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
		
	}

}
package dh07.run;

public class TestRun {
	public static void main(String[] args) {
		Runnable runnable = new RunThread();
		Thread thread1 = new Thread(runnable,"1号选手");
		Thread thread2 = new Thread(runnable,"2号选手");
		Thread thread3 = new Thread(runnable,"3号选手");
		Thread thread4 = new Thread(runnable,"4号选手");
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}

习题-多线程_第4张图片

------------------------------------------------------------------

5.网络购票

三人工抢十张,黄牛党只能抢一张。

package dh07.ticket;
//黄牛党只能抢1张票
public class TicketThread2 implements Runnable {
	private int ticket = 10;// 记录车票总数
	private int num = 0;// 记录用户抢到了第几张票

	public void run() {

		while (true) {
			if (qiangPiao()) {
				break;
			}

			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public synchronized boolean qiangPiao() {
		// 没有余票,退出循环
		if (ticket <= 0) {
			return true;
		}
		// 有余票,则抢票,第一步,修改车票数
		ticket--;
		num++;

		// 第二部:显示出票反馈给用户
		System.out.println(Thread.currentThread().getName() + "抢到了第" + num + "张票,剩余" + ticket + "张票。");
		if (Thread.currentThread().getName().equals("黄牛党")) {
			return true;
		}
		return false;
	}

}
package dh07.ticket;
//黄牛党只能抢1张票
public class TestThread2 {

	public static void main(String[] args) {
		Runnable runnable = new TicketThread2();
		Thread t1 = new Thread(runnable,"A");
		Thread t2 = new Thread(runnable,"b");
		Thread t3 = new Thread(runnable,"黄牛党");
		t1.start();
		t2.start();
		t3.start();
		
	}

}

习题-多线程_第5张图片

你可能感兴趣的:(Java核心编程API,java,开发语言)