线程:java中的使用

概念:

线程的创建:

通过new Thread()

 Thread T1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("守护线程T1在执行");
                }
            }
        });

线程的实现方式:

通过实现Runnable接口,重写run()方法,实现线程:
 Thread T1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("守护线程T1在执行");
                }
            }
        });
继承Thread类,重写run()方法,实现线程
  static class AddThread extends Thread {
        ReentrantLock lock = test2.reentrantLock;
        public void run() {
            for (int i = 0; i < 10000; i++) {
//          Counter.count2.incrementAndGet();//自增1
//               通过ReentrantLock加锁
                lock.lock();
                try {
                    Counter.count += 1;
                } finally {
//               通过ReentrantLock释放锁
                    lock.unlock();
                }
            }
        }
    }
实现Callable接口,重写call()方法,实现线程
//       创建callable

        MyCallAble callAble1 = new MyCallAble(1, 50);
        MyCallAble callAble2 = new MyCallAble(51, 100);

//       创建Future=>将callable转化为runable

        FutureTask<Integer> future1 = new FutureTask<>(callAble1);
        FutureTask<Integer> future2 = new FutureTask<>(callAble2);

        Thread thread1 = new Thread(future1);
        Thread thread2 = new Thread(future2);

        thread2.start();
        thread1.start();


//MyCallAble 类
  static class MyCallAble implements Callable<Integer> {
        private int begin, end;

        public MyCallAble(int begin, int end) {
            this.begin = begin;
            this.end = end;
        }

        @Override
        public Integer call() throws Exception {
            int it = 0;
            for (int i = begin; i <= end; i++) {
                it += i;
            }
            return it;
        }
    }
通过线程池创建并管理线程,需要使用线程时,向线程池提交线程任务,由线程池分配空闲线程去执行线程任务
//        ThreadPoolExecutor:线程池类
//        Executors类:工具类,用于创建各种线程
//        ExecutorService:定义线程池的行为
//       创建线程
        ExecutorService executorService = Executors.newFixedThreadPool(10);
//        提交线程任务
        for(int i=0;i<=10;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("线程池中的"+Thread.currentThread().getName()+"开始执行————————");
                }
            });
        }

//        关闭线程池

        executorService.shutdown();

线程的优先级:

默认为5,优先级最低为1,最高为10,具体执行顺序和优先级关系不大,还是操作系统分配时间片

线程状态

此处是针对java中的原码中的状态的解释,进入runable阶段可以分为两种状态:可运行状态,表示时刻准备着等分配,还有一种就是已经运行的状态

线程的中断

通过interrupt()方法,改变中断状态值,并通过抛出一个InterruptedException异常,中断线程执行

线程的让出

基于操作系统的线程抢占模型,线程会通过yield( ) 实现当前线程对CPU时间的让出

用户线程:主线程完成之后并不会直接结束用户线程(子线程)

守护线程:他通过setDemon(true)设置,主线程完了他也就结束了

1. 建立三个线程,并且同时运行它们。当运行时输出线程的名称。 实验步骤: (1)、创建类sy6_1 (2)、创建三个线程,调用start()方法启动这三个线程 (3)、保存文件,调试并编译运行程序。 参考程序运行效果: 2. 实现3个类:Storage、Counter和Printer。 Storage类应存储整数。 Counter应创建线程线程从0开始计数(0,1,2,3…)并将每个值存储到Storage类。 Printer类应创建一个线程线程读取Storage类的值并打印值。编写程序创建Storage类的实例,并创建一个Counter对象和Printer对象操作此实例。 实验步骤: (1)、创建三个类Counter, Printer,Storage (2)、创建TestCounter类,在该类定义main函数,在main函数定义Storage对象、Counter对象和 Printer对象,创建Counter线程和Printer线程并启动 (3)、保存文件,调试并编译运行程序。 参考程序运行效果: 3. 修改实验1第2题的程序,添加适当代码,以确保每个数字都恰好只被打印一次。 实验步骤: (1)、创建三个类Counter, Printer,Storage (2)、 创建TestCounter类,在该类定义main函数,在main函数定义Storage对象、Counter1对象和 Printer对象,创建Counter线程和Printer线程并启动 (3)、在定义Storage类的setValue(int i) 和getValue ()方法时使用synchronized关键字,将其定义为同步方法 (4)、保存文件,调试并编译运行程序。 参考程序运行效果:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值