线程创建

继承Thread类

package com.student_thread.demo;

public class TestThread1 extends Thread {//继承线程 类使我们自己创造的类也变成线程类

    @Override
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println("我在学进程"+i);
        }
    }
//main线程、主线程
    public static void main(String[] args) {
//创建一个线程对象
        TestThread1 testThread1=new TestThread1();

        //testThread1.start();//用start方法开启线程,主线程和子线程的执行顺序在任务多的时候会交替执行
       testThread1.start();//用run方法来开启线程,就会优先执行主线程,主线程和子线程的执行顺序即便在任务多的时候也是先执行完主线程再子线程
       // java里面的线程没有主和子的分别,但是有优先级的区别

        for (int i = 0; i < 20; i++) {
            System.out.println("我在学编程"+i);
        }
    }


}

实现Runnable接口

package com.student_thread.demo;

public class TestThread3 implements Runnable {//这里没有继承Thread是因为在后面使用了Thread线程类对象

    @Override
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println("我在学进程"+i);
        }
    }
    //main线程、主线程
    public static void main(String[] args) {
//创建一个线程对象
        TestThread3 testThread3=new TestThread3();
         //创建线程对象,通过线程对象来开启我们的线程,代理
        //16 Thread thread=new Thread();//虽然我们没有建Thread这个类,但是这个类并不需要我们自己建,这个是系统的线程类
       //17 thread.start();
       //16、17可以简化为:
        new Thread(testThread3).start();//将实例化对象直接作为参数传入线程并启动


        //testThread3.start();//用start方法开启线程,主线程和子线程的执行顺序在任务多的时候会交替执行
       // testThread3.run();//用run方法来开启线程,就会优先执行主线程,主线程和子线程的执行顺序即便在任务多的时候也是先执行完主线程再子线程
        // java里面的线程没有主和子的分别,但是有优先级的区别

        for (int i = 0; i < 20; i++) {
            System.out.println("我在学编程"+i);
        }
    }
}
实现callable接口
package com.student_thread.demo2;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

//第三种创建线程方法三,实现callable接口
public class TestCallable implements Callable<Boolean> {
        String url;
        String name;
        public TestCallable(String url,String name){
            this.url=url;
            this.name=name;
        }
        //线程执行体
        @Override
        public Boolean call() {
            WebDownloader  webDownloader =new WebDownloader();
            webDownloader.downloader(url,name);
            System.out.println("下载了文件"+name);
            return true;
        }


        public static void main(String[] args) throws Exception,InterruptedException{
            com.student_thread.demo.TestThread2 T1=new com.student_thread.demo.TestThread2("http://120.26.163.133/img/img.png","img.png");
            com.student_thread.demo.TestThread2 T2=new com.student_thread.demo.TestThread2("http://120.26.163.133/img/profile.jpg","profile.png");
            //创建执行服务
            ExecutorService ser = Executors.newFixedThreadPool(2);
            //提交执行
            Future<Boolean> r1 = (Future<Boolean>) ser.submit(T1);
            Future<Boolean> r2 = (Future<Boolean>) ser.submit(T2);
            //获取结果
            boolean rs1 = r1.get();
            boolean rs2 = r2.get();
            System.out.println(rs1);
            System.out.println(rs2);
            //关闭服务
            ser.shutdownNow();
        }

    //下载器
    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出现问题");
            }
        }
    }


}

 

posted @ 2021-12-02 14:54  (((^_^)))  阅读(44)  评论(0)    收藏  举报