java多线程基础

2017-09-30  本文已影响0人  花丶小伟

同步和异步的区别

进程介绍

不管是我们开发的应用程序,还是我们运行的其他的应用程序,都需要先把程序安装在本地的硬盘上。然后找到这个程序的启动文件,启动程序的时候,其实是电脑把当前的这个程序加载到内存中,在内存中需要给当前的程序分配一段独立的运行空间。这片空间就专门负责当前这个程序的运行。
不同的应用程序运行的过程中都需要在内存中分配自己独立的运行空间,彼此之间不会相互的影响。我们把每个独立应用程序在内存的独立空间称为当前应用程序运行的一个进程。
进程:它是内存中的一段独立的空间,可以负责当前应用程序的运行。当前这个进程负责调度当前程序中的所有运行细节。

线程介绍

启动的QQ聊天软件,需要和多个人进行聊天。这时多个人之间是不能相互影响,但是它们都位于当前QQ这个软件运行时所分配的内存的独立空间中。
在一个进程中,每个独立的功能都需要独立的去运行,这时又需要把当前这个进程划分成多个运行区域,每个独立的小区域(小单元)称为一个线程。
线程:它是位于进程中,负责当前进程中的某个具备独立运行资格的空间。
进程是负责整个程序的运行,而线程是程序中具体的某个独立功能的运行。一个进程中至少应该有一个线程。

多线程介绍

现在的操作系统基本都是多用户,多任务的操作系统。每个任务就是一个进程。而在这个进程中就会有线程。
真正可以完成程序运行和功能的实现靠的是进程中的线程。
多线程:在一个进程中,我们同时开启多个线程,让多个线程同时去完成某些任务(功能)。
(比如后台服务系统,就可以用多个线程同时响应多个客户的请求)
多线程的目的:提高程序的运行效率。

多线程运行的原理

cpu在线程中做时间片的切换。
其实真正电脑中的程序的运行不是同时在运行的。CPU负责程序的运行,而CPU在运行程序的过程中某个时刻点上,它其实只能运行一个程序。而不是多个程序。而CPU它可以在多个程序之间进行高速的切换。而切换频率和速度太快,导致人的肉眼看不到。
每个程序就是进程, 而每个进程中会有多个线程,而CPU是在这些线程之间进行切换。
了解了CPU对一个任务的执行过程,我们就必须知道,多线程可以提高程序的运行效率,但不能无限制的开线程。

实现多线程的三种方式

第一种:继承Thread类

public class MyThreadWithExtends extends Thread {
    String flag;
    
    public MyThreadWithExtends(String flag){
        this.flag = flag;
    }
    
    

    @Override
    public void run() {
        String tname = Thread.currentThread().getName();
        System.out.println(tname+"线程的run方法被调用……");
        Random random = new Random();
        for(int i=0;i<20;i++){
            try {
                Thread.sleep(random.nextInt(10)*100);
                System.out.println(tname+ "...."+ flag);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new MyThreadWithExtends("a");
        Thread thread2 = new MyThreadWithExtends("b");
        thread1.start();
        thread2.start();
        /**
         * 如果是调用thread的run方法,则只是一个普通的方法调用,不会开启新的线程
         */
//      thread1.run();
//      thread2.run();
    }
}

第二种:实现Runnable接口

public class MyThreadWithImpliment implements Runnable {
    int x;

    public MyThreadWithImpliment(int x) {
        this.x = x;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println("线程" + name + "的run方法被调用……");
        for (int i = 0; i < 10; i++) {
            System.out.println(x);
            try {
                Thread.sleep(100);
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new MyThreadWithImpliment(1), "thread-1");
        Thread thread2 = new Thread(new MyThreadWithImpliment(2), "thread-2");
         thread1.start();
         thread2.start();
        // 注意调用run和调用start的区别,直接调用run,则都运行在main线程中
//      thread1.run();
//      thread2.run();
    }
}

第三种:线程池

线程池的5种创建方式

线程池的使用

public class ThreadPoolWithRunable {

    
    /**
     * 通过线程池执行线程
     * @param args
     */
    public static void main(String[] args) {
        //创建一个线程池
        ExecutorService pool = Executors.newCachedThreadPool();
        for(int i = 1; i < 5; i++){
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("thread name: " + Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        pool.shutdown();
    }

}
public class ThreadPoolWithcallable {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newFixedThreadPool(4); 
        
        for(int i = 0; i < 10; i++){
            Future<String> submit = pool.submit(new Callable<String>(){
                @Override
                public String call() throws Exception {
                    //System.out.println("a");
                    Thread.sleep(5000);
                    return "b--"+Thread.currentThread().getName();
                }              
               });
            //从Future中get结果,这个方法是会被阻塞的,一直要等到线程任务返回结果
            System.out.println(submit.get());
        } 
            pool.shutdown();

    }

}
上一篇 下一篇

猜你喜欢

热点阅读