如何使用Java编写多线程程序

来源:IT专家网

  线程呢可以看作单独地占有CPU时间来执行相应的代码的。当然一个程序可以是多线程的,多线程的各个线程看上去像是并行地独自完成各自的工作,就像一台一台计算机上运行着多个处理机一样。

  1、什么是线程

  线程呢可以看作单独地占有CPU时间来执行相应的代码的。当然一个程序可以是多线程的,多线程的各个线程看上去像是并行地独自完成各自的工作,就像一台一台计算机上运行着多个处理机一样。在多处理机计算机上实现多线程时,它们确实可以并行工作,而且采用适当的分时策略可以大大提高程序运行的效率。

  2、为什么要使用多线程

  顺序执行程序和采用多线程并行执行程序相比,效率是可以大大地提高。

  3、java中是如何实现多线程

  在java语言中的线程系统是java语言自建的, 因为java中有专门的支持多线程的API库,所以很容易写一个支持线程的程序。在使用java创建线程的时候,可以生成一个Thread类或者他的子类对象,并给这个对象发送start()消息(程序可以向任何一个派生自 Runnable 接口的类对象发送 start() 消息的),这样一来程序会一直执行,直到run返回为止,此时该线程就停止了。

  在这里我们用多线程中最典型的例子,生产者与消费者问题。在这个例子里面我们定义了生产者Producer,消费者Consumer和仓库Warehouse三个类,在整个程序的生命周期里,生产者随机地制造出产品放到仓库中,消费者也是随即地从仓库中取出产品。

import exception.ProducerConsumerException;
  /**
  * Consumer.java
  * Consumer
  * By: Jiabo
  * Date: Mar 21, 2004
  * Time: 2:47:58 PM
  */
  public class Consumer extends Thread {
  private Warehouse warehouse;
  private String id;
  public Consumer(Warehouse warehouse, String id) {
  this.warehouse = warehouse;
  this.id = id;
  }
  public void run() {
  int tmp = (int) Math.random() * 10;
  try {
  warehouse.get(tmp);
  System.out.println("Consumer # " + this.id + " get " + tmp);
  } catch (ProducerConsumerException e) {
  e.printStackTrace();
  }
  try {
  sleep((int) (Math.random() * 100));
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  }

  在这个类中,值得注意的一点是run方法中必须使用try-catch,因为,消费者从仓库中取东西时有可能诸如仓库中的储量不够得异常,在消费者里面也是一样,只不过异常变为仓库已满。

  import exception.*;
  /**
  * Producer.java
  * Producer
  * By: Jiabo
  * Date: Mar 21, 2004
  * Time: 2:47:45 PM
  */
  public class Producer extends Thread {
  private Warehouse warehouse;
  private String id;
  public Producer(Warehouse warehouse, String id) {
  this.warehouse = warehouse;
  this.id = id;
  }
  public void run() {
  int tmp = (int) Math.random() * 10;
  if (tmp != 0) {
  try {
  warehouse.put(tmp);
  System.out.println("Consumer # " + this.id + " put " + tmp);
  } catch (ProducerConsumerException e) {
  e.printStackTrace();
  }
  }
  try {
  sleep((int) (Math.random() * 100));
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  }
  }

  最重要的一部分在Warehouse类,如上所说为了保证get何set的原子性,在这里使用了synchronized关键字,并且在操作时抛出了可能跑出的异常。

import exception.*;
  /**
  * Warehouse
  * By: Jiabo
  * Date: Mar 21, 2004
  * Time: 2:48:10 PM
  */
  public class Warehouse {
  // max capability of the warehouse
  private int MAX;
  private int contents;
  // init with max capacity
  public Warehouse(int max) {
  this.MAX = max;
  this.contents = 0;
  }
  public synchronized void get(int amount) throws ProducerConsumerException {
  // the amount you want to get is bigger than the contends that the warehouse stores
  if (amount > this.contents) {
  throw new NotEnoughGoodsException();
  }
  amount -= contents;
  }
  public synchronized void put(int amount) throws ProducerConsumerException {
  // the amount you want to put is out of the capability of the warehouse
  if (amount > (this.MAX - this.contents)) {
  throw new WarehouseFullException();
  } else if (this.contents == 0) {
  // warehouse is empty
  throw new WarehouseEmptyException();
  }
  amount += contents;
  }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值