Java多线程

参照狂神说java

线程

线程创建

  • 三种创建方式 Thread class、Runnable接口、Callable接口

  • Thread

  • package com.kuang.Demo01;
    
    //创建线程方式一:继承Thread类,重写run方法,调用start开启线程
    public class TestThread1 extends Thread{
        @Override
        public void run() {
            //run方法线程体
            for(int i = 0; i<2000;i++)
            {
                System.out.println("我在看代码---"+i);
            }
        }
    
        public static void main(String[] args) {
            //main线程,主线程
    
            TestThread1 testthread1 = new TestThread1();
            testthread1.start();
            for(int i = 0;i<2000;i++){
                System.out.println("我在学习多线程--"+i);
            }
        }
    }
    
  • //需要commoms.io包中的commoms.io.jar
    package com.kuang.Demo01;
    
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    
    //练习Thread, 实现多线程同步下载图片
    public class TestThread2 extends Thread{
        private String url;
        private String name;
    
        public TestThread2(String url,String name) {
            this.url = url;
            this.name = name;
        }
        @Override
        public void run() {
            WebDownloader webDownloader = new WebDownloader();
            webDownloader.downloader(url,name);
            System.out.println("下载了文件名为:"+name);
        }
    
        public static void main(String[] args) {
            TestThread2 t1 = new TestThread2("https://mt.lenovo.com.cn/c/471/1616989475423.jpg","2.jpg");
            TestThread2 t2 = new TestThread2("https://mt.lenovo.com.cn/c/471/1616989475423.jpg","3.jpg");
    
    
            t1.start();
            t2.start();
        }
    }
    
    //下载器
    class WebDownloader{
        public void downloader(String url,String name){
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("IO异常,downloader方法出现问题");
            }
        }
    }
    
  • 实现Runnable

    package com.kuang.Demo01;
    
    //创建线程方式2:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法。
    public class TestThread3 implements Runnable{
        @Override
        public void run() {
            //run方法线程体
            for(int i = 0; i<2000;i++)
            {
                System.out.println("我在看代码---"+i);
            }
        }
    
        public static void main(String[] args) {
            //创建runnable接口的实现类对象
            TestThread3 testThread3 = new TestThread3();
            //
            Thread thread = new Thread(testThread3);
            thread.start();
            for(int i = 0; i < 1000; i++){
                System.out.println("我在学习多线程--"+i);
            }
        }
    }
    
    //存在并发问题
    package com.kuang.Demo01;
    
    //多个线程同时操作同一个对象
    //买火车票的例子
    public class TestThread4 implements Runnable{
        //票数
        private int ticketNums = 10;
        @Override
        public void run() {
            while(true){
                if(ticketNums<=0)
                {
                    break;
                }
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
            }
        }
    
        public static void main(String[] args) {
            TestThread4 ticket = new TestThread4();
            new Thread(ticket,"小牛").start();
            new Thread(ticket,"小明").start();
            new Thread(ticket,"黄牛党").start();
    
        }
    }
    
  • 实现callable

    package com.kuang.Demo01;
    
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.util.concurrent.*;
    
    //callable可以定义返回值,可以抛出异常
    public class TestCallable implements Callable<Boolean> {
        private String url;
        private String name;
    
        public TestCallable(String url,String name) {
            this.url = url;
            this.name = name;
        }
        @Override
        public Boolean call() {
            WebDownloader1 webDownloader = new WebDownloader1();
            webDownloader.downloader(url,name);
            System.out.println("下载了文件名为:"+name);
            return true;
        }
    
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            TestCallable t1 = new TestCallable("https://mt.lenovo.com.cn/c/471/1616989475423.jpg","2.jpg");
            TestCallable t2 = new TestCallable("https://mt.lenovo.com.cn/c/471/1616989475423.jpg","3.jpg");
    
            //创建执行服务:
            ExecutorService ser = Executors.newFixedThreadPool(2);
    
            //提交执行
            Future<Boolean> r1 = ser.submit(t1);
            Future<Boolean> r2 = ser.submit(t2);
    
            //获取结果
            boolean rs1 = r1.get();
            boolean rs2 = r2.get();
    
            //关闭服务
            ser.shutdownNow();
        }
    }
    
    //下载器
    class WebDownloader1{
        public void downloader(String url,String name){
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("IO异常,downloader方法出现问题");
            }
        }
    }
    
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页