JavaEE-什么是多线程?(Thread类的基本用法)

Thread 类的常见构造方法

class  MyRunnable implements Runnable{
    
    
    @Override
    public void run() {
    
    
   }
}
        Thread t1=new Thread();                    //创建线程对象
        Thread t2 = new Thread(new MyRunnable());  //使用 Runnable 对象创建线程对象
        Thread t3 =new Thread(new MyRunnable(),"线程3"); //使用 Runnable 对象创建线程对象,并命名
        Thread t4 = new Thread("线程4");           //创建线程对象,并命名

1.启动一个线程

通过重写run方法,创建一个线程对象,但线程创建出来后并没有运行起来,得通过start方法进行线程启动。

class MyThread extends Thread{
    
    
    @Override
    public void run() {
    
    
        System.out.println("新线程");
    }
}
public class test4 {
    
    
    public static void main(String[] args) {
    
    
        Thread t=new MyThread();
        t.start();                    //线程开始运行
    }
}

2.中断一个线程

中断线程通常有两种方法:

通过共享的标记来进行中断

public class ThreadDemo2 {
    
    
    static class MyRunnable1 implements Runnable{
    
    
        public static boolean isQuit=false;
        @Override
        public void run() {
    
    
            while (!isQuit){
    
    
                System.out.println("新创建的线程");
            }
        }
    }
    public static void main(String[] args) {
    
    
        MyRunnable1 target =new MyRunnable1();
Thread t =new Thread(target);
t.start();
        try {
    
    
            Thread.sleep(1000);
        } catch (InterruptedException e) {
    
    
            throw new RuntimeException(e);
        }
target.isQuit=true;
    }
}

调用 interrupt() 方法来进行中断

interrupt()方法是判断当前线程的中断标志位是否设置,调用后清除标志位。

public class ThreadDemo6 {
    
    
    public static class MyRunnable implements Runnable{
    
    
        @Override
        public void run() {
    
    
            while (!Thread.interrupted()){
    
    
                System.out.println("新线程");
            }
        }
    }
    public static void main(String[] args) {
    
    
        MyRunnable myRunnable = new MyRunnable();
        Thread t=new Thread(myRunnable);
        t.start();
        try {
    
    
            Thread.sleep(3*1000);
        } catch (InterruptedException e) {
    
    
            throw new RuntimeException(e);
        }
        t.interrupt();
    }
}

3.等待一个线程

public class ThreadDemo4 {
    
    
    public static void main(String[] args) {
    
    
        Thread t = new Thread(()->{
    
    
            System.out.println("新线程");
        });
        t.start();
        try {
    
    
            t.join();
        } catch (InterruptedException e) {
    
    
            throw new RuntimeException(e);
        }
    }
}

mian线程等待新创建的t线程执行完后,main线程才能结束。

4.获取当前线程的引用

返回当前线程对象的引用

public class ThreadDemo5 {
    
    
    public static void main(String[] args) {
    
    
        Thread t =Thread.currentThread();
        System.out.println(t.getName());
    }
}

在这里插入图片描述
通过返回mian线程的引用,打印main线程的名称。

5.休眠线程

下面我们通过调用System.currentTimeMillis()方法来查看休眠主线程一定时间后当前时间的变化与休眠时间是否相同

public class ThreadDemo {
    
    
public static void main(String[] args) throws InterruptedException {
    
    
System.out.println(System.currentTimeMillis());   //以毫秒为单位返回当前时间
Thread.sleep( 1000);
System.out.println(System.currentTimeMillis());   //以毫秒为单位返回当前时间
}
}

在这里插入图片描述
在这里插入图片描述
所以,这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的。

猜你喜欢

转载自blog.csdn.net/st200112266/article/details/129785664
今日推荐