javase笔记12----线程2
线程常用的属性方法
public static void main(String[] args){
//main方法本质上就是一个线程
//1.获取当前线程的对象
Thread current = Thread.currentThread();
//2.获取当前线程的名字
String name = current.getName();
//3.获取当前线程的唯一标识符
long id = current.getId();
//4.获取当前线程的优先级
int priority = current.getPriority();
//5.获取当前线程的状态
Thread.State state = current.getState();
//6.查看当前线程是否存活
boolean alive = current.isAlive();
//7.查看当前线程是否被打断
boolean interrupted = current.isInterrupted();
//8.查看当前线程是否是守护线程
boolean daemon = current.isDaemon();
}
守护线程
1.线程要么是后台线程,要么是前台线程;
2.前台线程:在表面运行的,能看到的。Daemon的值为false。
3.后台线程:就是守护线程,即Daemon的值为true。所有的前台线程结束了,守护线程就会立即结束。
public static void main(String[] args){
Thread rose = new Thread("rose"){
public void run(){
for(int i = 1;i<6;i++){
System.out.println(getName()+"说: I will jump...");
}
System.out.println("落水中");
}
};
Thread jack = new Thread("jack"){
public void run(){
for(int i = 1;i<10;i++){
System.out.println(getName()+"说:ou jump,I jump..");
}
}
};
//jack是守护线程
jack.setDaemon(true);
rose.start();
jack.start();
}
声明周期相关方法
sleep()方法
static void sleep(long millis)
线程睡眠方法,使线程转到阻塞状态。millis参数设定睡眠的时间,以毫秒为单位。当睡眠结束后,就转为就绪状态。
休眠时间内,可能会被异常中断,所以要处理异常
public static void main(String[] args){
Thread t = new Thread("download"){
public void run(){
for(int i = 1;i<=10;i++){
System.out.println("正在下载中..."+(i*10)+"%");
//使用休眠,模拟下载视频
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
};
t.start();
}
yield()方法
static void yield()
线程让步方法,暂停当前正在执行的线程对象,使之处于可运行状态,把执行机会让给相同或者更高优先级的线程。
public static void main(String[] args){
Thread t1 = new Thread("Thread-A"){
public void run(){
for(int i = 0;i<10;i++){
System.out.println(getName()+":"+i);
try{
Thread.sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
};
Thread t2 = new Thread("Thread-B"){
publiic void run(){
for(int i = 0;i<10;i++){
//打印5之前让一下时间片段
if(i==5){
//Thread-B让出cpu,进入就绪状态。
Thread.yield();
}
System.out.println(getName()+":"+i);
try{
Thread.sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
};
t1.start();
t2.start();
}
join()方法
void join()
线程加入方法,等待其他线程终止。在当前线程中调用另一个线程的join()方法,则当前线程转入阻塞状态,直到另一个进程运行结束,当前线程再由阻塞转为就绪状态
public static void main(String[] args){
Thread dowanload = new Thread("download"){
public void run(){
for(int i = 1;i<10;i++){
System.out.println(getName()+"图片正在下载中,进度:"+i+"%");
}
System.out.println("图片下载完成...");
}
};
Thread show = new Thread("show"){
public void run(){
try{
//下载线程执行完毕后显示线程才能执行
download.join();
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
System.out.println("图片显示完成...");
}
};
download.start();
show.start();
}
interrupt()方法
void interrupt()
线程打断方法。 打断哪个线程,就用哪个线程对象调用。
public static void main(String[] args){
Thread lan = new Thread("懒羊羊"){
public void run(){
System.out.println("大家午安我要睡觉啦~");
try{
Thread.sleep(100000);
}catch((InterruptedException e){
e.printStackTrace();
System.out.println("灰太狼你好吵啊!");
}
}
};
Thread hui = new Thread("灰太狼"){
public void run(){
for(int i = 0;i<10;i++){
System.out.println("懒羊羊起床啦!!");
try{
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("你的青草蛋糕被我吃掉啦!");
lan.interrupt();
}
}
};
lan.start();
hui.start();
}