• 【笔记】java多线程 1


    在java中要想实现多线程,主要有两种手段,第一是直接继承Thread类,第二是实现Runable接口。

    直接继承Thread类,大致的框架式:

    1 public class Test extends Thread{
    2      //方法1
    3      //方法2
    4      public void run()
    5      {
    6       }
    7 }

    下面我们写个例子

     1 public class Test extends Thread{
     2     private String name;
     3     public Test(){
     4         
     5     }
     6     public Test(String name){
     7         this.name = name;
     8     }
     9     public void run(){
    10         for(int i =0;i<5;i++){
    11             System.out.println(name+"运行      "+i);
    12         }
    13     }
    14     
    15     public static void main(String[] args){
    16         Test t1 = new Test("A");
    17         Test t2 = new Test("B");
    18         t1.run();
    19         t2.run();
    20     }
    21 }

    运行之后得到的结果是:

    A运行 0
    A运行 1
    A运行 2
    A运行 3
    A运行 4
    B运行 0
    B运行 1
    B运行 2
    B运行 3
    B运行 4

    通过结果我们发现,虽然是多线程,但是AB线程是顺序执行的,并没有达到多线程的效果。

    试着将主函数中的run改成start。

    1 public static void main(String[] args){
    2         Test t1 = new Test("A");
    3         Test t2 = new Test("B");
    4         t1.start();
    5         t2.start();
    6     }

     再次运行后,得到的结果是:

    A运行 0
    A运行 1
    A运行 2
    A运行 3
    A运行 4
    B运行 0
    B运行 1
    B运行 2
    B运行 3
    B运行 4

    这下结果就正确了。

    其实start还是调用run方法,只不过它新启用了一个线程,让此线程处于就绪(可运行)状态,并没有运行,一旦cpu有空闲,就开始执行run方法。

    run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行。

    通过Runable接口实现的多线程

    大致的框架式:

    1 public class Test implements Runnable{
    2    //方法1
    3    //方法2
    4    public void run{}
    5 }

    具体的例子

     1 public class Test implements Runnable{
     2     private String name;
     3     public Test(){
     4         
     5     }
     6     public Test(String name){
     7         this.name = name;
     8     }
     9     public void run(){
    10         for(int i =0;i<5;i++){
    11             System.out.println(name+"运行      "+i);
    12         }
    13     }
    14     
    15     public static void main(String[] args){
    16         Test t1 = new Test("A");
    17         Thread h1 = new Thread(t1);
    18         Test t2 = new Test("B");
    19         Thread h2 = new Thread(t2);
    20         h1.start();
    21         h2.start();
    22     }
    23 }

    其实Runable接口调用的run方法,就是Thread类调用的run方法,它们之间的调用模式就涉及到代理模式。

    关于代理模式,可以参考这个:http://www.cnblogs.com/rollenholt/archive/2011/08/18/2144847.html

    实现Runnable接口比继承Thread类所具有的优势:

    1):适合多个相同的程序代码的线程去处理同一个资源

    2):可以避免java中的单继承的限制

    3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。

  • 相关阅读:
    C#
    C#
    ssh学习笔记
    (已解决)Could not open '/var/lib/nova/mnt/*/volume-*': Permission denied
    RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in major version and less than or equal in minor version as the specified version cap 4.11.
    如何在linux下安装idea
    The system has no LUN copy license
    调整mysql数据库最大连接数
    mysql数据库编码问题
    cinder支持nfs快照
  • 原文地址:https://www.cnblogs.com/nonkicat/p/2685077.html
Copyright © 2020-2023  润新知