线程就是 程序运行的一条线路。
1.新线程的创建
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<20;i++){
System.out.println(123);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread th=new Thread(new ThreadTest().new Mythread());
th.start();//开启线程
}
2.Thread 的方法的介绍
start()开启线程
boolean isAlive();判断线程是否活着
final String getName(); 获得线程名
setName(String name) ; 设置线程名;
setPriority(int Priproty) 设置线程的优先级1-10; 数字越大优先级越高
sleep(long mills);线程休眠
interrupt();线程中断
getState() 获得线程的状态
currentthread() 获得当前线程的对象
set Daemon(boolean on );标记为守护线程 或用户线程
thread.join 加入线程
Thread.yield();线程让步