JAVA 线程

程序是静态的。

Process:进程,程序执行的过程,动态的概念,是系统资源分配的单位

Thred:线程,线程是CPU调度和执行的单位,线程开启不一定立即执行,等待CPU调度才执行

1、mainc:用户线程
2、gc:守护线程

三种线程创建方式:
1、继承Thread类(重点)

**public class thread01 extends Thread{
    @Override
    public void run() {
        // run方法线程题
        for (int i = 0; i < 20; i++) {
            System.out.println("我在看代码"+i );
        }

    }

    public static void main(String[] args) {
        // main方法,主线程
        //创建一个线程对象,调用start开启线程
        thread01 test = new thread01();
        test.start(); //两个方法同时调起,交替执行
     //   test.run(); //先走run方法
        for (int i = 0; i < 20; i++) {
            System.out.println("我在学习多线程 "+i);
        }
    }
}
**
package Kuang;

import org.apache.commons.io.FileUtils;
import sun.net.www.protocol.file.FileURLConnection;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Thread02 extends Thread {
    private String url;
    private String name;

    public Thread02(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) {
        Thread02 test1 = new Thread02("https://dcloud.pingan.com.cn/img/features/top.png", "文件1.jpg");
        Thread02 test2 = new Thread02("https://dcloud.pingan.com.cn/img/features/top.png", "文件2.jpg");
        Thread02 test3 = new Thread02("https://dcloud.pingan.com.cn/img/features/top.png", "文件3.jpg");
        test1.start();
        test2.start();
        test3.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("I/O异常,downloader方法出现问题:" + name);
        }

    }
}




2、实现Runnable接口(重点)

package Kuang;

public class thread03 implements Runnable{
    @Override
    public void run() {
        // run方法线程题
        for (int i = 0; i < 100; i++) {
            System.out.println("我在看代码"+i );
        }
    }

    public static void main(String[] args) {
        // main方法,主线程
        //创建一个线程对象,调用start开启线程
        thread03 test = new thread03();
        new Thread(test).start(); //两个方法同时调起,交替执行
        //   test.run(); //先走run方法
        for (int i = 0; i < 1000; i++) {
            System.out.println("我在学习多线程 "+i);
        }
    }
}

3、实现Callable接口(了解)
定义返回值&可以抛出异常

package Kuang;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

public class ThreadCallable implements Callable<Boolean> {
    private String url;
    private String name;

    public ThreadCallable(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 {
        ThreadCallable test1 = new ThreadCallable("https://dcloud.pingan.com.cn/img/features/top.png", "文件1.jpg");
        ThreadCallable test2 = new ThreadCallable("https://dcloud.pingan.com.cn/img/features/top.png", "文件2.jpg");
        ThreadCallable test3 = new ThreadCallable("https://dcloud.pingan.com.cn/img/features/top.png", "文件3.jpg");
        ExecutorService er1 = Executors.newFixedThreadPool(3);
        Future<Boolean> r1 = er1.submit(test1);
        Future<Boolean> r2 = er1.submit(test2);
        Future<Boolean> r3 = er1.submit(test3);
        boolean r1s=r1.get();
        boolean r2s=r2.get();
        boolean r3s=r3.get();
        er1.shutdown();

    }
}

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("I/O异常,downloader方法出现问题:" + name);
        }

    }
}

ArrayList、HashSet/LinkedHashSet、PriorityQueue、LinkedList是线程不安全的,

继承Thread对象

子类具备多线程能力;
启动:子类对象.start();
不建议使用,避免OOP单继承局限性

实现Runnable接口

子类具备多线程能力;
启动:传入子类对象+Thread对象.start();
推荐使用:避免单继承局限性,灵活方便,方便对一个对象被多个线程使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值