线程的状态一共有5种 分别为新生状态,就绪状态,运行状态,阻塞状态,死亡状态。
线程的的使用方式:
线程的使用方式有很多种,这里只简单介绍几种
1.优先级
public final void setPriority(int newPriority) 更改线程的优先级。 默认为5,最小为1,最大为10 设置了优先级别之后,级别高 并不是说你一定被优先调度,而是你的被优先调度的概率高而已。 public class Test { public static void main(String[] args) { Tuzi tz=new Tuzi("兔子"); tz.setPriority(2); tz.start(); Wugui wg=new Wugui("乌龟"); wg.setPriority(8); wg.start(); } } public class Wugui extends Thread { @Override public void run() { for (int i = 1; i <=100; i++) { System.out.println("我是乌龟我在跑。。"+"----"+this.getName()+"---"+this.getPriority()); } } public Wugui(String name) { super(name); } }
2.线程强制运行:join()
可以通过join()方法使得一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成之后,才可以继续运行。
public final void join() throws InterruptedException
等待该线程终止。
public final void join(long millis)throws InterruptedException
等待该线程终止的时间最长为 millis 毫秒。超时为 0 意味着要一直等下去。
package Thread1;
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class demo1{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化Runnable子类对象
Thread t = new Thread(mt,"线程"); // 实例化Thread对象
t.start() ; // 启动线程
for(int i=0;i<50;i++){
if(i>10){
try{
t.join() ; // 线程强制运行
}catch(InterruptedException e){}
}
System.out.println("Main线程运行 --> " + i) ;
}
}
};
3 线程的休眠(sleep)
在线程中允许一个线程进行暂时的休眠,直接使用Thread.sleep()方法即可。
sleep定义格式:
public static void sleep(long milis,int nanos)
throws InterruptedException
首先,static,说明可以由Thread类名称调用,其次throws表示如果有异常要在调用此方法处处理异常。
所以sleep()方法要有InterruptedException 异常处理,而且sleep()调用方法通常为Thread.sleep(500) ;形式。
控制当前线程休眠若干毫秒
* 1秒= 1000毫秒
* 1秒 = 1000 * 1000 * 1000纳秒 1000000000
package Thread1;
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<50;i++){
try{
Thread.sleep(500) ; // 线程休眠
}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class demo1{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化Runnable子类对象
Thread t = new Thread(mt,"线程"); // 实例化Thread对象
t.start() ; // 启动线程
}
};
4.线程的礼让(yield)
yield()方法实现线程的礼让。
package Thread1;
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<5;i++){
try{
Thread.sleep(500) ; //休眠一下
}catch(Exception e){}
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
if(i==2){
System.out.print("线程礼让:") ;
Thread.currentThread().yield() ; // 首先获取当前线程,然后线程礼让
}
}
}
};
public class demo1{
public static void main(String args[]){
MyThread my = new MyThread() ; // 实例化MyThread对象
Thread t1 = new Thread(my,"线程A") ;
Thread t2 = new Thread(my,"线程B") ;
t1.start() ;
t2.start() ;
}
};
5.判断线程是否在执行:isAlive
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<3;i++){
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class ThreadAliveDemo{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化Runnable子类对象
Thread t = new Thread(mt,"线程"); // 实例化Thread对象
System.out.println("线程开始执行之前 --> " + t.isAlive()) ; // 判断是否启动
t.start() ; // 启动线程
System.out.println("线程开始执行之后 --> " + t.isAlive()) ; // 判断是否启动
for(int i=0;i<3;i++){
System.out.println(" main运行 --> " + i) ;
}
// 以下的输出结果不确定
System.out.println("代码执行之后 --> " + t.isAlive()) ; // 判断是否启动
}
};
6.当前线程:CurrentThread()
程序可以通过currentThread()方法取得当前正在运行的线程对象,
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<3;i++){
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class CurrentThreadDemo{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化Runnable子类对象
new Thread(mt,"线程").start() ; // 启动线程
mt.run() ; // 直接调用run()方法
}
};
7.线程名称
1,在Thread类中可以通过getName()方法取得线程名称,通过setName()设置线程名称。
2,线程的名称一般在启动线程前设置,但也允许为运行的线程设置名称,允许两个Thread对象有相同名称,但是应该避免。
3,如果程序没有为线程指定名称,系统会自动为线程设置名称。
class MyThread implements Runnable{ // 实现Runnable接口
public void run(){ // 覆写run()方法
for(int i=0;i<3;i++){
System.out.println(Thread.currentThread().getName()
+ "运行,i = " + i) ; // 取得当前线程的名字
}
}
};
public class ThreadNameDemo{
public static void main(String args[]){
MyThread mt = new MyThread() ; // 实例化Runnable子类对象
new Thread(mt).start() ; // 系统自动设置线程名称
new Thread(mt,"线程-A").start() ; // 手工设置线程名称
new Thread(mt,"线程-B").start() ; // 手工设置线程名称
new Thread(mt).start() ; // 系统自动设置线程名称
new Thread(mt).start() ; // 系统自动设置线程名称
}
};