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();
}
}