JAVA高级基础(65)---线程控制方法及获取系统当前线程

线程名称

设置名称:Thread(Runnable target,String name)
                 getName();返回线程名称
                 getId();获取线程的ID
                 setName(String name);设置线程名称

在创建线程时,系统会默认为每个线程分配一个名称:Thread-0  Thread-1

获取当前执行线程

                       Thread.currentThread();返回当前执行线程的对象

线程休眠

                        sleep(long m)休眠时间的单位:毫秒

守护线程

                      setDeamon(boolean on);  on 为true 的时候,该线程为守护线程
                      isDeamon() 判断一个线程是否为守护线程
                      isAlive() 判断一个线程是否为活动线程

package org.lanqiao.thread.demo;

public class ThreadName {
	public static void main(String[] args) {
		Thread  t1 = new Thread("线程一") {
			public void run() {
				/*try {
                                        //线程休眠5秒
					Thread.sleep(5000);  
				} catch (InterruptedException e) {
					e.printStackTrace();
				}*/
				for(int i= 0 ; i < 5; i++) {
					
					System.out.println(Thread.currentThread().getName());
				}
			}
			
		};
		t1.setName("线程壹");
		
		System.out.println( t1.getName());
		System.out.println(t1.getId());
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i= 0 ; i < 5; i++) {
					if(i % 3 == 0) {
						try {
							t1.join();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					
					System.out.println(Thread.currentThread().getName());
				}
			}
			
			
		},"线程二");
		t2.setName("线程贰");
		System.out.println(t2.getName());
		System.out.println(t2.getId());
		System.out.println("---------------------");
		//获取当前执行线程对象
		Thread  currentThread = Thread.currentThread();
		System.out.println(currentThread.getName());
                //守护线程
		t1.setDaemon(true);
		t1.start();
		System.out.println("------t1"+t1.isAlive());
		System.out.println("t1"+t1.isDaemon());
		t2.start();
		System.out.println("----t2"+t2.isAlive());
		System.out.println("t2"+ t2.isDaemon());
	}
}

加入线程

                      join();加入线程,当前执行线程会暂停,直到加入的线程执行完成之后,才会继续执行当前线程

应用:如果在做个线程执行时,想让另一个线程优先执行,可以将该线程加入到当前线程中。

package org.lanqiao.thread.demo;

public class TestJoin {
	public static void main(String[] args) {
		Thread t1 = new Thread("线程1") {
			public void run() {
				for(int i = 0 ; i < 50000 ; i++) {
					System.out.println(Thread.currentThread().getName() +"执行次数" + i);	
					}
				}
			
		};
		
		
		Thread t2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i = 0 ; i < 50000 ; i++) {
					System.out.println(Thread.currentThread().getName() +"执行次数" + i);
					if(i % 3 == 0 ) {
						try {
							t1.join(1);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		},"线程2");
		t2.start();
		t1.start();
	}
}

礼让线程

                     static  yield();使当前线程出让CPU的执行权。

设置线程的优先级   

java 中采用抢占式的线程调度方式:线程的优先级使从1--10 ,数字越大,优先级越高。优先级高代表该线程获得CPU的执行权的几率更大。
        •setPriority()设置线程的优先级
        •getPriority()获取线程的优先级   

package org.lanqiao.thread.demo;

public class TestYield {
	public static void main(String[] args) {
		Thread t1 = new Thread("线程1") {
			public void run() {
				for(int i = 0 ; i < 5 ; i++) {
					System.out.println(Thread.currentThread().getName() +"执行次数" + i);	
					}
				}
			
		};
		
		
		Thread t2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i = 0 ; i < 5; i++) {
					System.out.println(Thread.currentThread().getName() +"执行次数" + i);
					if(i % 3 == 0 ) {
						try {
							Thread.yield();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		},"线程2");
		System.out.println(t1.getPriority());
		t1.setPriority(10);
		t2.setPriority(1);
		t2.start();
		System.out.println(t2.getPriority());
		t1.start();
	}
}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值