zoukankan      html  css  js  c++  java
  • Java 并发编程实践基础 读书笔记: 第一章 JAVA并发编程实践基础

    1.创建线程的方式:

     1 /**
     2  * StudySjms
     3  * <p>
     4  * Created by haozb on 2018/2/28.
     5  */
     6 public class ThreadDemo extends Thread {
     7     /**
     8      * 定义线程的名字
     9      * @param name
    10      */
    11     public ThreadDemo(String name) {
    12         super(name);
    13     }
    14 
    15     @Override
    16     public void run() {
    17         for (int i = 0; i < 20; i++) {
    18             System.out.println(this.getName()+":"+i);
    19         }
    20     }
    21 
    22     public static void main(String[] args) {
    23         for (int i = 0; i < 5; i++) {
    24             new ThreadDemo("name"+i).start();
    25         }
    26     }
    27 }
     1 /**
     2  * StudySjms
     3  * <p>
     4  * Created by haozb on 2018/2/28.
     5  */
     6 public class ThreadDemo implements Runnable {
     7     @Override
     8     public void run() {
     9         for (int i = 0; i < 5; i++) {
    10             System.out.println(Thread.currentThread().getName()+":"+i);
    11         }
    12     }
    13 
    14     public static void main(String[] args) {
    15 /*        Runnable demo = new ThreadDemo();
    16         for (int i = 0; i < 3; i++) {
    17             new Thread(demo,""+i).start();
    18         }*/
    19         
    20         // 也可以直接采用内名内部类方式来创建
    21         final int num = 5;
    22         for (int i = 0; i < 3; i++) {
    23             new Thread(new Runnable() {
    24                 @Override
    25                 public void run() {
    26                     for (int i = 0; i < num; i++) {
    27                         System.out.println(Thread.currentThread().getName()+":"+i);
    28                     }
    29                 }
    30             },""+i).start();
    31         }
    32     }
    33 }

    Executors其实就是工厂类,用来创建各种的线程池;

     1 import java.util.concurrent.*;
     2 
     3 /**
     4  * StudySjms
     5  * <p>
     6  * Created by haozb on 2018/2/28.
     7  */
     8 public class ThreadPool {
     9     public static void main(String[] args) {
    10         //ExecutorService pool = Executors.newFixedThreadPool(2);
    11         // 这种是固定线程数的线程池,控制了现场的最大并发量
    12         //ExecutorService pool = Executors.newCachedThreadPool();
    13         // 这种没有长度限制,会自动控制池的大小,如果任务多了,会创建新现场,如果任务少了,会回收线程
    14         //ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
    15         /*
    16         这种例子是延迟3秒执行
    17         pool.schedule(new Runnable() {
    18             @Override
    19             public void run() {
    20 
    21             }
    22         },3,TimeUnit.SECONDS);*/
    23         /*
    24         这种是延迟1秒 每3秒执行一次
    25         pool.scheduleAtFixedRate(new Runnable() {
    26             @Override
    27             public void run() {
    28 
    29             }
    30         },1,3,TimeUnit.SECONDS);*/
    31         /*
    32         相当于单线程执行任务,有顺序    
    33         ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    34         */
    35 
    36         ExecutorService pool = new ThreadPoolExecutor(5,100,0L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>());
    37         for (int i = 0; i < 10; i++) {
    38             final int tmp = i;
    39             Runnable run = new Runnable() {
    40                 @Override
    41                 public void run() {
    42                     long time = (long)(Math.random()*1000);
    43                     System.out.println(Thread.currentThread().getName()+":"+tmp+"sleeping"+time);
    44 
    45                     try {
    46                         Thread.sleep(time);
    47                     } catch (InterruptedException e) {
    48                         e.printStackTrace();
    49                     }
    50                 }
    51             };
    52             pool.execute(run);
    53         }
    54         pool.shutdown();
    55     }
    56 }

    线程的基本控制方法:

    Thread.sleep();   使线程睡眠,放弃cpu

    join :使一个线程等待另一个线程完事在执行。eg: t1里面,t2.join()   t1暂停执行,立即执t2,t2执行完事之后再去执行t1

      可以有参数,t2.join(10);  表示为t1只等t210毫秒就继续执行了

    interrupt():中断线程,有些时候需要在线程执行过程中中断线程。

  • 相关阅读:
    【51nod1674】区间的价值 V2(算法效率--位运算合并优化+链表实现)
    【bzoj 2339】[HNOI2011]卡农(数论--排列组合+逆元+递推)
    关于中国剩余定理{附【转】中国剩余定理 }
    JavaScript操作BOM
    学员操作—统计考试平均成绩
    JavaScript基础
    JDBC
    进制
    事务
    复习
  • 原文地址:https://www.cnblogs.com/haoerlv/p/8484576.html
Copyright © 2011-2022 走看看