java多线程整理

一、创建线程

1.继承Thread类

package demo4thread;

public class CreateThread {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
            if (i == 20) {
                new demo4thread1().start();
                new demo4thread1().start();
            }
        }
    }
}

class demo4thread1 extends Thread{
    
    private int n = 0;
    public void run(){
        for (; n < 100; n++) {
            System.out.println(n);
        }
    }
}


2.实现runable接口

创建runable的实现类a,创建a类的实例b,以b作为线程的target创建线程对象c,c才是真正的线程对象。

public class CreateThread {

    public static void main(String[] args) {
        for(int i=0;i<100;i++)
        {
            System.out.println(Thread.currentThread().getName()+" "+i);
            if(i==20){
                demo4thread2 demo4thread2 = new demo4thread2();
                new Thread(demo4thread2,"thread1").start();;
                new Thread(demo4thread2,"thread2").start();;
            }
        }
    }
}

class demo4thread2 implements Runnable{

    private int i;
    public void run()
    {
        for(;i<100;i++)
        {
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
    }
    
}

 

采用实现Runnable接口的方式创建多线程的优缺点:
优势:(1)线程类只是实现了Runnable接口,还可以继承其他类。
           (2)在这种方式下,多个线程可以共享一个target对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU、代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
劣势:编程稍稍复杂,如果需要访问当前线程,则必须使用Thread.currentThread()方法。
采用继承Thread类的方法创建多线程的优缺点:
劣势:因为线程类已经继承了Thread类,所以不能再继承其他父类。
优势:编写简单,如果需要访问当前线程,则无须使用Thread.currentThread()方法,直接使用this即可获得当前线程。
鉴于上面分析,因此一般推荐采用实现Runnable接口、Callable接口的方式来创建多线程。

2.wait()、sleep()、yield()、join()

3.什么是线程池,如何使用

线程池就是事先将多个线程对象放到一个容器中,当使用时不用new线程而是直接去池中拿即可。节省了开辟子线程的时间,提高代码执行效率。

newCachedThreadPool
创建一个可缓存线程池,应用中存在的线程数可以无限大

package demo4thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyThreadPool {

    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        System.out.println("****************************newCachedThreadPool*******************************");
        for(int i=0;i<4;i++)
        {
            final int index=i;
          newCachedThreadPool.execute(new ThreadForpools(index));
        }
    }
}

class ThreadForpools implements Runnable{

    private Integer index;
    public  ThreadForpools(Integer index)
    {
     this.index=index;
    }
    @Override
    public void run() {
        /***
         * 业务......省略
          */
        try {
            System.out.println("开始处理线程!!!");
            Thread.sleep(index*100);
            System.out.println("我的线程标识是:"+this.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

newFixedThreadPool
创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

package demo4thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyThreadPool {

    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = Executors.newFixedThreadPool(2);
        System.out.println("****************************newCachedThreadPool*******************************");
        for(int i=0;i<4;i++)
        {
            final int index=i;
          newCachedThreadPool.execute(new ThreadForpools(index));
        }
    }
}

class ThreadForpools implements Runnable{

    private Integer index;
    public  ThreadForpools(Integer index)
    {
     this.index=index;
    }
    @Override
    public void run() {
        /***
         * 业务......省略
          */
        try {
            System.out.println("开始处理线程!!!");
            Thread.sleep(index*100);
            System.out.println("我的线程标识是:"+this.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


4.线程池的处理过程

线程池初次创建时,池中没有任何线程。当任务发布时,线程池会进行判断,选择立即创建线程去执行任务还是将任务放至队列。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值