Thread类的基本用法

1.线程创建

        Thread类是JVM用来管理线程的一个类,换句话说,每个线程都有一个唯一的Thread对象与之关联(java代码中的Thread对象和操作系统中的线程是一一对应的)

1.1.继承Thread类,重写run方法

package asd;
class MyThread extends Thread{
    @Override
    public void run() {
        while(true){
            System.out.println("hello t");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
public class Test3 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();

        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

1.2  继承Thread类,重写run方法,使用匿名内部类

package asd;

public class Test4 {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                while (true){
                    System.out.println("hello t");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 1.3  实现Runnable类,重写run方法

package asd;
class MyRunnable implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("hello t");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Test5 {
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        Thread t = new Thread(runnable);
        t.start();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 1.4  实现Runnable类,重写run方法,使用匿名内部类方法

package asd;

public class Test6 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("hello t");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.线程中断

本质让来说,让一个线程终止,办法只有一种,让该线程的入口方法执行完毕.

2.1给线程中设定一个结束标志位:

        死循环导致入口方法无法执行完毕,所以不能结束线程.

package asd;

public class Test7 {
    public static boolean isQuit = false;

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
           while (!isQuit){
               System.out.println("hello t");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
            System.out.println("t 线程终止");
        });
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isQuit = true;
    }
}

 Thread类内置了一个标志位,让我们更方便的实现上述效果

如果要结束循环,就得在catch中写一个break

package asd;

public class Test8 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
           while (!Thread.currentThread().isInterrupted()){
               System.out.println("hello t");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
                   try {
                       Thread.sleep(2000);
                   } catch (InterruptedException interruptedException) {
                       interruptedException.printStackTrace();
                   }
                   break;
               }
           }
        });
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
}

 3.线程等待

join方法是在main线程中调用join方法,意思就是让main线程等待t线程先结束后,在执行.

package asd;

public class Test9 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() ->{
            System.out.println("hello t");
        });
        t.start();
        t.join();
        System.out.println("hello main");
    }
}

 4.线程休眠

sleep方法使线程进入休眠状态,并设定休眠的时间

public static void sleep(long millis) throws InterruptedException        休眠当前线程  millis毫秒

public static void sleep(long millis,int nanos) throws InterruptedException

5.获取线程实例

Thread.currentThead()就能够获取到当前线程的引用(Thread实例的引用)

package asd;

public class Test10 {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                System.out.println(this.getName());
            }
        };
        t.start();
    }
}

 线程的名字只能通过Thread.getName().

package asd;

public class Test10 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
        t.start();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值