java多线程实例

线程休眠实例

第一种方式:

public void Test(){
	public static void main(String[] args){
		MyRunnable myRunnable = new MyRunnable();
		Thread thread = new Thread(myRunnable);
		try{
			thread.sleep(3000);		//先暂停3秒在启动
		}catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thread.start();
	}
}

=========================================================================

public class MyRunnable implements Runnable {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 100; i++) {
			System.out.println("------MyRunnable");
		}
	}
}

第二种方式:

public class Test {
	public static void main(String[] args) {
		MyRunnable myRunnable = new MyRunnable();
		Thread thread = new Thread(myRunnable);
		thread.start();
	}
}

=========================================================================

public class MyRunnable implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 100; i++) {
			try {
				Thread.currentThread().sleep(3000);	//每执行一次暂停3秒
													//Thread.currentThread() 是指获取当前运行的线程对象
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("------MyRunnable");
		}
	}
}

第三种方式:

public class Test {
	public static void main(String[] args) {
		MyThread myThread = new MyThread();
		myThread.start();
	}
}

=========================================================================

public class MyThread extends Thread {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 100;i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("------Thread");
		}
	}
}

第四种方式

public class Test {
	public static void main(String[] args) {
		MyThread myThread = new MyThread();
		
		try{
			MyThread.sleep(3000);
		}catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		myThread.start();
	}
}

=========================================================================

public class MyThread extends Thread {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i = 0; i < 100;i++) {
			System.out.println("------Thread");
		}
	}
}

返回上一层


线程合并实例

public class Test {
	public static void main(String[] args) {
		JoinRunnable joinRunnable = new JoinRunnable();
		Thread thread = new Thread(joinRunnable);
		thread.start();
		for (int i = 0; i < 100; i++) {
			if(i == 10) {
				try {
					thread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(i+"Test+++++++++++++++++");
		}
	}
}
=======================================================================
class JoinRunnable implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(i+"------------JoinRunnable");
		}
	}
	
}


返回上一层


线程礼让实例


public class Test{
	public static void main(String[] args) {
		YieldThread1 y1 = new YieldThread1();
		y1.setName("YieldThread1");
		YieldThread2 y2 = new YieldThread2();
		y2.setName("YieldThread2");
		y1.start();
		y2.start();
	}
}

class YieldThread1 extends Thread{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			if(i == 5) {
				yield();
			}
			System.out.println(getName()+"------"+i);
		}
	}
}

class YieldThread2 extends Thread{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 30; i++) {
			System.out.println(getName()+"------"+i);
		}
	}
}

返回上一层


package day_4_13;

public class Test {
	public static void main(String[] args) {
		A a = new A();
		Thread thread = new Thread(a);
		thread.start();
		System.out.println("现在线程的状态:" + thread.getState());
		System.out.println("\n中断线程");
		thread.interrupt();
		System.out.println("线程现在是否处于中断状态(注意,只有线程启动了才有中断一说。\n如果连线程连启动都没有那还中断个锤锤) \n" + thread.isInterrupted());
		System.out.println("\n现在线程的状态:" + thread.getState());
		
	}
}

class A implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i =0;i<100;i++) {
			System.out.println("奔跑吧 ! 皮卡丘");
		}
	}
	
}



返回上一层

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值