Java多线程的相关机制

一 线程的基本概念

成都创新互联为您提适合企业的网站设计 让您的网站在搜索引擎具有高度排名,让您的网站具备超强的网络竞争力!结合企业自身,进行网站设计及把握,最后结合企业文化和具体宗旨等,才能创作出一份性化解决方案。从网站策划到做网站、成都网站建设, 我们的网页设计师为您提供的解决方案。

线程是一个程序内部的顺序控制流.一个进程相当于一个任务,一个线程相当于一个任务中的一条执行路径.;多进程:在操作系统中能同时运行多个任务(程序);多线程:在同一个应用程序中有多个顺序流同时执行;Java的线程是通过java.lang.Thread类来实现的;JVM启动时会有一个由主方法(public static void main(){})所定义的线程;可以通过创建Thread的实例来创建新的线程;每个线程都是通过某个特定Thread对象所对应的方法run()来完成其操作的,方法run()称为线程体,通过调用Thread类的start()方法来启动一个线程。

二 线程的创建和启动

可以有两种方式创建新的线程:
***种:
     1.定义线程类实现Runnable接口
     2.Thread myThread = new Thread(target);   //target为Runnable接口类型
     3.Runnable中只有一个方法:public void run();用以定义线程运行体
     4.使用Runnable接口可以为多个线程提供共享的数据
     5.在实现Runnable接口的类的run()方法定义中可以使用Thread的静态方法public static Thread currentThread();获取当前线程的引用
   
第二种:
      1.可以定义一个Thread的子类并重写其run方法如:
          class MyThread extends Thread {   
              public void run() {...}
            
          }   
     2.然后生成该类的对象:
         MyThread myThread = new MyThread();

三 线程控制的基本方法

isAlive():判断线程是否还"活"着
getPriority():获得线程的优先级数值
setPriority():设置线程的优先级数值
Thread.sleep():将当前线程睡眠指定毫秒数
join():调用某线程的该方法,将当前线程与该线程"合并",即等待该线程结束,再恢复当前线程的运行
yield():让出cpu,当前线程进入就绪队列等待调度
wait():当前线程进入对象的wait pool
notify()/notifyAll():唤醒对象的wait pool中的一个/所有等待线程

四 线程同步

实现生产者消费者问题来说明线程问题,举例如下所示:

  1. /**  
  2. * 生产者消费者问题  
  3. */ 
  4. package com.basic.thread;  
  5. /**  
  6. * @author johnston678  
  7. *  
  8. * @version 2009-05-06  
  9. */ 
  10. public class ProducerConsumer {  
  11.      /**  
  12.       * @param args  
  13.       */ 
  14.      public static void main(String[] args) {          
  15.          ProductBox pb = new ProductBox();  
  16.          Producer p = new Producer(pb);  
  17.          Consumer c = new Consumer(pb);  
  18.           
  19.          Thread pThread = new Thread(p);  
  20.          Thread cThread = new Thread(c);  
  21.          pThread.setPriority(Thread.MAX_PRIORITY);  
  22.           
  23.          pThread.start();  
  24.          cThread.start();  
  25.      }  
  26. }  
  27. /**  
  28. * 产品对象  
  29. * @author johsnton678  
  30. */ 
  31. class Product {  
  32.      int id;  
  33.      public Product(int id) {  
  34.          super();  
  35.          this.id = id;  
  36.      }  
  37.       
  38.      public String toString(){  
  39.          return "Product:" + id;  
  40.      }  
  41. }  
  42. /**  
  43. * 产品盒对象  
  44. * @author johnston678  
  45. */ 
  46. class ProductBox {  
  47.      Product[] productbox = new Product[6];  
  48.      int index = 0;  
  49.      public ProductBox() {  
  50.          super();          
  51.      }  
  52.       
  53.      public synchronized void push(Product p) {  
  54.          while (index == productbox.length) {  
  55.              try {  
  56.                  this.wait();  
  57.              } catch (InterruptedException e) {  
  58.                  // TODO Auto-generated catch block  
  59.                  e.printStackTrace();  
  60.              }  
  61.          }  
  62.          this.notify();          
  63.          productbox[index] = p;  
  64.          index ++;  
  65.      }  
  66.       
  67.      public synchronized Product pop() {  
  68.          while (index == 0) {  
  69.              try {  
  70.                  this.wait();  
  71.              } catch (InterruptedException e) {  
  72.                  // TODO Auto-generated catch block  
  73.                  e.printStackTrace();  
  74.              }  
  75.          }  
  76.          this.notify();  
  77.          index --;  
  78.          return productbox[index];  
  79.           
  80.      }  
  81. }  
  82. /**  
  83. * 生产者  
  84. * @author johnston678  
  85. */ 
  86. class Producer implements Runnable {  
  87.      ProductBox productbox = null;  
  88.       
  89.      public Producer(ProductBox productbox) {  
  90.          super();  
  91.          this.productbox = productbox;  
  92.      }  
  93.      @Override 
  94.      public void run() {  
  95.          // TODO Auto-generated method stub  
  96.          for (int i=0; i<10; i++) {  
  97.              Product p = new Product(i);  
  98.              productbox.push(p);  
  99.              System.out.println("produce:" + p);  
  100.               
  101.              try {  
  102.                  Thread.sleep((int)(Math.random() * 200));  
  103.              } catch (InterruptedException e) {  
  104.                  e.printStackTrace();  
  105.              }  
  106.          }  
  107.      }  
  108.       
  109. }  
  110. /**  
  111. * 消费者  
  112. * @author johnston678  
  113. */ 
  114. class Consumer implements Runnable {  
  115.      ProductBox productbox = null;  
  116.       
  117.      public Consumer(ProductBox productbox) {  
  118.          super();  
  119.          this.productbox = productbox;  
  120.      }  
  121.      @Override 
  122.      public void run() {  
  123.          // TODO Auto-generated method stub  
  124.          for (int i=0; i<10; i++) {  
  125.              Product p = productbox.pop();  
  126.              System.out.println("consume:" + p);  
  127.               
  128.              try {  
  129.                  Thread.sleep((int)(Math.random() * 1000));  
  130.              } catch (InterruptedException e) {  
  131.                  e.printStackTrace();  
  132.              }  
  133.          }  
  134.      }  
  135.       

 

网页标题:Java多线程的相关机制
当前链接:http://www.shufengxianlan.com/qtweb/news23/45623.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联