1、线程基础、线程之间的共享和协作

基础概念:

cpu核心数 和 线程数的关系:

1:1;超线程技术 - > 1:2

 

cpu时间片轮转机制:

rr调度,上下文切换

 

什么是进程和线程:

进程:程序运行资源分配的最小单位,进程内部有多个线程,多个线程共享这个进程的资源;

线程:CPU调度的最小单位,

 

澄清并行和并发:

并发:与时间单位相关;单位时间内可以处理事情的能力;

并行:同一时刻,可以处理事情的能力;

高并发编程的意义,好处和注意事项:

意义

1、可以充分利用CPU 的资源;

2、加快用户相应时间;

3、代码模块化,异步化;

注意事项:

共享资源,存在冲突,死锁,

太多的线程 可能搞垮机器;

 

java程序天生就是多线程;

public class OnlyMain {
    public static void main(String[] args) {
        //虚拟机线程管理的接口,通过这个类 可以拿到当前应用程序里有多少线程;
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);

        for (ThreadInfo threadInfo : threadInfos) {
            System.out.println(threadInfo.getThreadId() + "   " + threadInfo.getThreadName());
        }
        /**
         * 输出结果:
         *
         *  5   Monitor Ctrl-Break      统计当前程序运行时相关信息
            4   Signal Dispatcher       分发处理发送给虚拟机的信号
            3   Finalizer               调用对象方法
            2   Reference Handler       负责清除引用
            1   main                    用户程序入口
         *
         */
    }
}

开启新线程的几种方式?

类 Thread

接口 Runnable

接口 Callable

 

public class NewThread {

    //实现 Runnable 接口
    public static class UseRun implements Runnable{

        @Override
        public void run() {
            System.out.println("I am implements Runnable");
        }
    }

    //实现 Callable 接口 允许有返回值
    public static class UseCall implements Callable<String>{

        @Override
        public String call() throws Exception {
            System.out.println("I am implements Callable");
            return "CallResult";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        UseRun useRun = new UseRun();
        new Thread(useRun).start();
        UseCall useCall = new UseCall();
        FutureTask<String> futureTask = new FutureTask<String>(useCall);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }
}

java线程是协作式的

怎么才能让 java 里的线程安全停止?

1、自然执行完

2、抛出异常

stop();、resume();、suspend(); 线程不会释放资源;死锁

interrupt(); 线程方法、中断线程,并不是强行关闭这个线程,中断标志位 置为 true

isInterrupted(); 判定当前线程是否处于中断状态

static 方法 interrupted(); 判定当前线程是否处于中断状态 中断标志位 置为 false

public class EndThread {

    private static class UseThread extends Thread{

        public UseThread(String name){
            super(name);
        }

        public void run(){
            String name = Thread.currentThread().getName();
            while (!interrupted()){
                System.out.println(name + " is run !");
            }
            System.out.println(name + "interrupted is " + interrupted());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread endThread = new UseThread("endThread");
        endThread.start();
        Thread.sleep(20);
        endThread.interrupt();
    }

}

如何安全的中断线程?

方法抛出 InterruptedException ,线程的中断标志位会被 复位成false

要想正常终止必须在 捕获会抛出异常的代码块 catch里手动 interrupt();

public class HasInterruptedException {
    private static class UseThread extends Thread{

        public UseThread(String name){
            super(name);
        }

        public void run(){
            String name = Thread.currentThread().getName();
            while (!interrupted()){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(name + "interrupted in InterruptedException is " + interrupted());
                    interrupt();
                    e.printStackTrace();
                }
                System.out.println(name + " is run !");
            }
            //while 循环 自然终止
            System.out.println(name + "interrupted is " + interrupted());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread endThread = new UseThread("endThread");
        endThread.start();
        Thread.sleep(20);
        endThread.interrupt();
    }
}

线程的优先级:

1~10 、 缺省为 5 ;

setPriority() 设置优先级;

不一定优先级越高执行次数越多;

 

守护线程:

和主线程 共死;finally 不能保证一定执行

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值