线程中简单的方法

start()和run()

Start()方法:使线程开始执行,java虚拟机中调用该线程的run()方法。

run()方法:如果该线程是使用独立的Runnable()运行对象构造的,则执行Runnable对象的run方法;否则什么都不执行,值得一提的是,一般我们继承的Thread类已经实现了Runnable接口。

 运行的结果

 

main...运行...
Thread-0...运行...

可以发现start才是启动了一个线程,而run只是调用了类中的方法。

getName和setName方法

getName方法获取当前线程的名字

setName方法设置当前线程的名字

 运行结果是:

t1
t1...运行...

 值得注意的是,run方法中不能直接调用getName方法

join和yield方法

join方法时暂停当前线程,调用其他线程,如果带参数的,则暂停参数所给值得时间,之后继续运行。

class DemoThread extends Thread{
	public void run(){
		for(int i = 0; i<10;i++){
			System.out.println(Thread.currentThread().getName()+"-"+i+"...运行...");
		}
	}
}
public class demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DemoThread demoThread = new DemoThread();
		demoThread.setName("t1");
		demoThread.start();
		for(int j =0;j<10;j++){
			if(j == 1){
				try {
					demoThread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName()+"-"+j+"...运行...");
		}
		
	}

}

运行结果 

main-0...运行...
t1-0...运行...
t1-1...运行...
t1-2...运行...
t1-3...运行...
t1-4...运行...
t1-5...运行...
t1-6...运行...
t1-7...运行...
t1-8...运行...
t1-9...运行...
main-1...运行...
main-2...运行...
main-3...运行...
main-4...运行...
main-5...运行...
main-6...运行...
main-7...运行...
main-8...运行...
main-9...运行...

yield方法是使当前线程释放cpu从运行状态转为就绪状态,从剩余线程的优先级高的选取运行,但是如果只有当前一个线程的话,当先线程又会获得cpu的执行权限。

class DemoThread extends Thread{
	public void run(){
		for(int i = 0; i<10;i++){
			System.out.println(Thread.currentThread().getName()+"-"+i+"...运行...");
			if(i==4){
				Thread.yield();
			}
		}
		
	}
}
public class demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DemoThread demoThread = new DemoThread();
		demoThread.setName("t1");
		
		DemoThread demoThread2 = new DemoThread();
		demoThread2.setName("t2");
		
		demoThread.start();
		demoThread2.start();
	}

}

运行结果

t1-0...运行...
t1-1...运行...
t1-2...运行...
t1-3...运行...
t2-0...运行...
t1-4...运行...
t2-1...运行...
t1-5...运行...
t1-6...运行...
t1-7...运行...
t2-2...运行...
t1-8...运行...
t1-9...运行...
t2-3...运行...
t2-4...运行...
t2-5...运行...
t2-6...运行...
t2-7...运行...
t2-8...运行...
t2-9...运行...

yield方法不会释放同步锁 我们试着加上同步锁

class DemoThread extends Thread{
	private static Object obj = new Object();
	
	public  void run(){
		
		synchronized(obj){
			for(int i = 0; i<10;i++){
				System.out.println(Thread.currentThread().getName()+"-"+i+"...运行...");
				if(i==4){
					Thread.currentThread().yield();
				}
			}
		}
		
		
	}
}
public class demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DemoThread demoThread = new DemoThread();
		demoThread.setName("t1");
		
		DemoThread demoThread2 = new DemoThread();
		demoThread2.setName("t2");
		
		demoThread.start();
		demoThread2.start();
	}

}

运行结果

t1-0...运行...
t1-1...运行...
t1-2...运行...
t1-3...运行...
t1-4...运行...
t1-5...运行...
t1-6...运行...
t1-7...运行...
t1-8...运行...
t1-9...运行...
t2-0...运行...
t2-1...运行...
t2-2...运行...
t2-3...运行...
t2-4...运行...
t2-5...运行...
t2-6...运行...
t2-7...运行...
t2-8...运行...
t2-9...运行...

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值