zoukankan      html  css  js  c++  java
  • 1.传统线程技术的回顾-JDK5多线程

     
     1 import org.junit.Test;
     2 /**
     3  * 传统线程回顾
     4  * 多线程不一定会提高运行效率,和CPU设计和架构有关。
     5  * 多线程下载是抢了服务器的资源,并不是自身的机器加快。
     6  * @author LiTaiQing
     7  */
     8 public class TraditionThread {
     9     /**
    10      * 两种传统线程的创建方式
    11      */
    12     @Test
    13     public void test1(){
    14         //方式一
    15         Thread thread = new Thread(){
    16             /**
    17              * 从Thread.class源代码中发现,run方法在默认情况下是执行默认target.run();
    18              *  @Override
    19                 public void run() {
    20                     if (target != null) {
    21                         target.run();
    22                     }
    23                 }
    24                 
    25                 private Runnable target;
    26              */
    27             @Override
    28             public void run() {
    29                 while(true){
    30                     System.out.println(Thread.currentThread().getName());
    31                     //不要使用this,因为在Runnable创建线程的方式中this是无效的
    32                     //System.out.println(this.getName());
    33                 }
    34             }
    35         };
    36         thread.start();
    37         /**
    38          * 方式二
    39          * 使用Runnable更好,更加符合面向对象程序设计的思想,程序结构的耦合性更低,将任务和宿主分离
    40          */
    41         Thread thread2 = new Thread(new Runnable(){
    42             @Override
    43             public void run() {
    44                 while(true){
    45                     System.out.println(Thread.currentThread().getName());
    46                     //报错
    47                     //System.out.println(this.getName());
    48                 }
    49             }
    50         });
    51         thread2.start();
    52         //-------------------------------------
    53         /**
    54          * 考虑回运行哪一块代码?
    55          */
    56         new Thread(new Runnable(){
    57             @Override
    58             public void run() {
    59                 while(true){
    60                     try {
    61                         Thread.sleep(500);
    62                     } catch (InterruptedException e) {
    63                         e.printStackTrace();
    64                     }
    65                     System.out.println("Runable:"+Thread.currentThread().getName());
    66                 }
    67             }
    68         }){
    69             /**
    70              *     会运行此代码块,子类覆盖了父类的方法 ,那么就先运行子类,否则就去找父类    
    71              */
    72             @Override
    73             public void run() {
    74                 while(true){
    75                     try {
    76                         Thread.sleep(500);
    77                     } catch (InterruptedException e) {
    78                         e.printStackTrace();
    79                     }
    80                     System.out.println("Run:"+Thread.currentThread().getName());
    81                 }
    82             }
    83         }.start();
    84     }
    85 }
  • 相关阅读:
    Jmeter之检查点
    Jmeter之集合点
    Jmeter之参数化
    Jmeter组件认识
    Jmeter目录认识
    sts的web工程创建
    Jmeter的BeanShell脚本开发
    Jmeter插件开发
    Appium配置app老是反复安装问题的处理
    eclipse通过git代码的下载和上传
  • 原文地址:https://www.cnblogs.com/litaiqing/p/4635173.html
Copyright © 2011-2022 走看看