| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
tianzhi
7年前发布

java多线程开发基础

   <p>对于一个java程序猿来说,多线程开发技术无疑是需要掌握的。近期,小生在阅读高洪岩先生编写的《java多线程变成核心技术》一书。由于自己对于多线程开发了解的不多,而且在开发中使用的不多,希望通过阅读此书,并提炼出书中重点知识以及总结自己的一些感悟来提升自己多于多线程技术的理解。还望各位大神多多指教哈!</p>    <p><strong>1、进程与线程的概念与区别:</strong></p>    <p><strong>1.1 进程:</strong>进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位。 简单的理解,进程就是我们常说的应用程序。一般来说,一个应用也就是一个进程,但也可以有多进程的应用 。例如在Android开发中,常常会有一个后台常驻的进程,这就属于多进程。</p>    <p><strong>1.2 线程:</strong>线程是进程的一个实体, <strong>线程属于进程,依附于进程存在</strong> 。线程是 <strong>CPU调度和分派的基本单位</strong> 。线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它与同属一个进程的其他的线程共享进程所拥有的全部资源。 <strong>一个进程至少拥有一个线程,可以有多个线程存在。</strong></p>    <p><strong>2、Java中多线程的使用方式:</strong></p>    <p><strong>2.1继承Thread类:</strong></p>    <pre>  <code class="language-java">public class MyThread extends Thread{       @Override        public void run() {           System.out.println ("MyThread自定义线程运行");        }  }  //调用  MyThread myThread = new MyThread();  myThread.start();</code></pre>    <p><strong>2.2 实现Runnable:</strong></p>    <pre>  <code class="language-java">public class MyRunnable implements Runnable{           @Override          public void run() {                      System.out.println("MyRunnable自定义线程运行");            }  }  //调用  MyRunnable myRunnable = new MyRunnable();  Thread thread = new Thread(myRunnable);  thread.start();</code></pre>    <p><strong>3、多线程类中常用的一些方法:</strong></p>    <p><strong>3.1 currentThread()方法:</strong>获取执行当前代码段的线程信息。</p>    <p>下面通过实例来说明:</p>    <pre>  <code class="language-java">public class MyThread extends Thread{        public MyThread() {               System.out.println("MyThread构造方法开始执行------");           System.out.println("Thread.currentThread().getName():   "+Thread.currentThread().getName());           System.out.println("this.getName():   "+this.getName());           System.out.println("MyThread构造方法执行结束------");      }     @Override      public void run() {                System.out.println("run方法开始执行------");            System.out.println("run方法打印:   "+Thread.currentThread().getName());            System.out.println("this.getName():   "+this.getName());            System.out.println("MyThread构造方法执行结------");     }  }      //调用      public static void main(String[] args) {               MyThread myThread = new MyThread();           myThread.setName("mythread");           Thread thread = new Thread(myThread);           thread.setName("A");           System.out.println("------------------------------------");           thread.start();    }  //打印结果  MyThread构造方法开始执行------  Thread.currentThread().getName():   main  this.getName():   Thread-0  MyThread构造方法执行结束------  ------------------------------------  run方法开始执行------  run方法打印:   A  this.getName():   mythread  MyThread构造方法执行结------</code></pre>    <p>分析:构造方法在main方法,也就是在main线程中调用。因此, <strong>Thread.currentThread()</strong> 获取的是调用构造方法所在的线程,也就是main线程。而 <strong>this</strong> 指的是该类当前对象,也就是在main方法中通过new关键字创建的对象。</p>    <p><strong>3.2 isAlive()方法:</strong>判断当前线程是否处于活动状态(线程已经启动但是尚未终止)。</p>    <p><strong>3.3 sleep()方法:</strong>Thread类中的静态方法。让当前“正在执行的线程”休眠(暂停执行),该线程指的是this.currentThread()返回的线程。</p>    <p><strong>3.4 wait()方法:</strong>Object类中的方法。该方法使得当前线程进入"预执行队列",线程在wait()处等待,直到该线程接收到notify通知才会继续执行。或者是被中断才会停止。(该方法在后续讲解线程通信时会详细说明,此处只是为了与sleep对比)</p>    <p>wait方法调用前该线程必须获取对象锁,调用之后会释放该对象的锁</p>    <p>sleep方法调用之后不会释放该对象的锁</p>    <p><strong>3.5 线程暂停和停止的相关方法:</strong></p>    <p>Thread类中提供了比较多的方法来暂停或者停止线程,例如:stop、suspend,以及判断线程是否停止的方法,interrupted、isInterrupted等。不过jdk中已经不推荐使用stop相关方法停止线程。比较好的方式是: <strong>在run方法中通过具体业务逻辑来结束线程的运行。</strong></p>    <p>结束语</p>    <p>小生第一次在简书上写文章,内容相对来说会比较简单,后续我会坚持写下去。希望能够帮助到那些想要了解java多线程开发的童鞋。请大家多多支持哈。在此跪谢了哈。 <em>I will always work hard</em></p>    <p> </p>    <p>来自:http://www.jianshu.com/p/ca309a082a5c</p>    <p> </p>    
 本文由用户 tianzhi 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1480494510309.html
多线程 Java Java开发