java线程

Posted Jansens

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java线程相关的知识,希望对你有一定的参考价值。

//军队线程
//模拟作战双方的行为
public class ArmyRunnable implements Runnable {

	//volatile保证了线程可以正确的读取其他线程写入的值
	//可见性 ref JMM, happens-before原则
	volatile boolean keepRunning = true;

	@Override
	public void run() {
		while(keepRunning){
			//发动5连击
			for(int i=0;i<5;i++){
				System.out.println(Thread.currentThread().getName()+"进攻对方["+i+"]");
				//让出了处理器时间,下次该谁进攻还不一定呢!
				Thread.yield();
			}
					
		}
		
		System.out.println(Thread.currentThread().getName()+"结束了战斗!");

	}

}

  

public class KeyPersonThread extends Thread {

	public void run(){
		System.out.println(Thread.currentThread().getName()+"开始了战斗!");
		for(int i=0;i<10;i++){
			System.out.println(Thread.currentThread().getName()+"左突右杀,攻击隋军...");
		}
		System.out.println(Thread.currentThread().getName()+"结束了战斗!");
	}
}

  

/**
 * 隋唐演义大戏舞台
 */
public class Stage extends Thread {

	public void run(){
		
		System.out.println("欢迎观看隋唐演义");
		//让观众们安静片刻,等待大戏上演
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		System.out.println("大幕徐徐拉开");
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		System.out.println("话说隋朝末年,隋军与农民起义军杀得昏天黑地...");
		
		ArmyRunnable armyTaskOfSuiDynasty = new ArmyRunnable();
		ArmyRunnable armyTaskOfRevolt = new ArmyRunnable();
		
		//使用Runnable接口创建线程
		Thread  armyOfSuiDynasty = new Thread(armyTaskOfSuiDynasty,"隋军");
		Thread  armyOfRevolt = new Thread(armyTaskOfRevolt,"农民起义军");
		
		//启动线程,让军队开始作战
		armyOfSuiDynasty.start();
		armyOfRevolt.start();
		
		//舞台线程休眠,大家专心观看军队厮杀
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("正当双方激战正酣,半路杀出了个程咬金");
		
		Thread  mrCheng = new KeyPersonThread();
		mrCheng.setName("程咬金");
		
		System.out.println("程咬金的理想就是结束战争,使百姓安居乐业!");
		
		//停止军队作战
		//停止线程的方法
		armyTaskOfSuiDynasty.keepRunning = false;
		armyTaskOfRevolt.keepRunning = false;
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		/*
		 * 历史大戏留给关键人物
		 */
		mrCheng.start();
		
		//万众瞩目,所有线程等待程先生完成历史使命
		try {
			mrCheng.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
		System.out.println("战争结束,人民安居乐业,程先生实现了积极的人生梦想,为人民作出了贡献!");
		System.out.println("谢谢观看隋唐演义,再见!");
		
	}
	
	public static void main(String[] args) {
		new Stage().start();

	}

}

  

public class Actor extends Thread {

	public void run(){
		System.out.println(getName() + "是一个演员");

		int count = 0;

		boolean keepRunning = true;

		while (keepRunning) {

			System.out.println(getName() + "登台演出:" + (++count));

			if (count == 100) {

				keepRunning = false;
			}
			
			if(count%10==0){
				
				try {
					
					Thread.sleep(1000);
					
				} catch (InterruptedException e) {
					
					e.printStackTrace();
				}
			}
		}

		System.out.println(getName() + "的演出结束了");
	}
	
	public static void main(String[] args) {
		Thread actor = new Actor();
		actor.setName("Mr.Thread");
		actor.start();
		
		Thread threads=new Thread(new Actrees(),"Mr.Runnable");
		threads.start();
		
	}	
}

class Actrees implements Runnable{

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "是一个演员");

		int count = 0;

		boolean keepRunning = true;

		while (keepRunning) {

			System.out.println(Thread.currentThread().getName() + "登台演出:" + (++count));

			if (count == 100) {

				keepRunning = false;
			}
			
			if(count%10==0){
				
				try {
					
					Thread.sleep(1000);
					
				} catch (InterruptedException e) {
					
					e.printStackTrace();
				}
			}
		}

		System.out.println(Thread.currentThread().getName() + "的演出结束了");
		
	}
	
}

  

以上是关于java线程的主要内容,如果未能解决你的问题,请参考以下文章

Java工程师面试题,二级java刷题软件

java线程

Java——线程池

Java线程池详解

Java线程池详解

Java 线程池详解