Thread类的基本用法

Java创建线程时有,5中代码的编写方式

1.继承Thread类,并重写run()方法

public class Demo1 {
    //定义一个继承来自Thread的内部类
    //定义一个MyThread类继承Thread类并重写Thread中的run方法
    static class MyThread extends Thread{
        @Override
        public void run() {
            while(true){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        //创建线程对象
        Thread thread = new MyThread();
        //启动线程
        thread.start();
        //thread.run();
        while(true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

2.实现Runnable类,并重写run()方法 

//创建MyRunnable实现Runnable接口
//并重写run()方法
class MyRunnable implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
        
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable =new MyRunnable();
        //runnable中没有start方法,不能直接启动线程
        //reunnable.start();
        Thread thread = new Thread(runnable);
        thread.start();
        while(true){
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

3.Thread匿名内部类

public class Demo3 {
public static void main(String[] args) {
    //通过匿名内部内的方式创建线程
    //创建了一个Thread的匿名的内部类
    Thread t = new Thread() {
        @Override
        public void run() {
            while(true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    //启动线程
    t.start();

    while(true) {
        System.out.println("hello main");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

4.Runnable匿名内部类

public class Demo4 {
public static void main(String[] args) throws InterruptedException {
    //创建一个为Runnable的匿名的内部类
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while(true){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    Thread t = new Thread(runnable);
    t.start();
    while(true){
        System.out.println("hello main");
        Thread.sleep(1000);
    }
}
}

5.常用,简化写法

public class Demo5 {
    public static void main(String[] args) {
        //通的方式简写
        Thread t = new Thread(() -> {
            while(true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //启动线程
        t.start();

        while(true) {
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

6.线程的中断

public class Demo7 {
    //通过变量来打断线程的进程
    private static boolean flag = true;
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while(flag){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        t.start();

        System.out.println("hello main");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        flag = false;
        System.out.println("让 t 线程结束");
    }
}

7.线程等待

public class Demo8 {
    //直接使用线程内置的标准位 isInterrupted()
    //Thread对象中,包含了一个boolean类型的变量,如果为false,说明没有人去尝试终止这个线程,如果为true,说明有人去尝试终止这个线程
    //这个变量的默认值是false
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            //currentThread() 可以获取到当前线程的引用
            //哪个线程调用了它,它就可以获取哪个线程的引用
            while(!Thread.currentThread().isInterrupted()){
                //由于这个currentThread()方法,是在后续 t start之后才执行的
                //所以这里的t 就是当前线程
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                    //提前唤醒
                    //sleep提前唤醒,触发异常之后,然后就会把isInterrupted的false设置为true
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    //打印异常调用栈,回到while循环
                    //打印异常之后,就会把isInterrupted的false设置为true
                    //循环不会结束
                    //触发异常后程序不会立马终止,是为了让程序员有更多的操作空间
                    break;
                }
            }
        });
        t.start();
        //在主线程中尝试终止t线程
        //调用这个方法,会将t线程中的isInterrupted的false设置为true
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t.interrupt();
        // while(true){
        //     System.out.println("hello main");
        //     try {
        //         Thread.sleep(1000);
        //     } catch (InterruptedException e) {
        //         // TODO Auto-generated catch block
        //         e.printStackTrace();
        //     }
        // }
    }
}

 9.获取线程

public class Demo10 {
    public static void main(String[] args) throws InterruptedException {
        //哪个线程调用currentThread,返回的就是哪个线程的引用
        Thread main = Thread.currentThread();
        Thread t = new Thread(() ->{
            //让 t 线程阻塞等待 main 线程
            try {
                System.out.println("t 线程等待之前");
                main.join();
                System.out.println("t 线程等待之后");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
        t.start();
        for(int i = 0;i < 5;i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}

10,线程休眠 

 

public class Demo9 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for(int i = 0; i < 5; i++) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    break;
                }
            }
        });
        t.start();

        //主线程中,就可以对 t 线程进行"等待"
        System.out.println("主线程等待前");

        t.join();
        //哪个线程中调用join,该线程就是"等的一方"(此处就是main)
        //join前面是哪个引用,对应的线程就是被等的一方(此处就是t)
        //main等待 t 结束
        //join的等待是一个死等,只要被等待的线程 t 没有结束,join就一直阻塞
        System.out.println("主线程等待后");
    }
}

 

 

 

 

 

你可能感兴趣的:(java,开发语言)