Java教程 专题
专题目录
您的位置:java > Java教程专题 > Java 多线程
Java 多线程
作者:--    发布时间:2019-11-20

java线程教程 - java多线程


以下代码显示了如何在程序中运行多线程。

public class main {
  public static void main(string[] args) {
    // create two thread objects
    thread t1 = new thread(main::print);
    thread t2 = new thread(main::print);

    // start both threads
    t1.start();
    t2.start();
  }

  public static void print() {
    for (int i = 1; i <= 500; i++) {
      system.out.println(i);
    }
  }
}

上面的代码生成以下结果。

线程同步

java编程语言内置了两种线程同步:

  • 互斥同步
  • 条件同步

在互斥同步中,在一个时间点只允许一个线程访问代码段。

条件同步通过条件变量和三个操作来实现:等待,信号和广播。


同步关键字

synchronized关键字用于声明需要同步的关键部分。

有两种方法可以使用synchronized关键字:

  • 将方法声明为关键部分
  • 将语句块声明为关键段

我们可以通过在方法的返回类型之前使用关键字synchronized来声明一个方法作为临界段。

public class main {
  public synchronized void somemethod_1() {
    // method code goes here
  }

  public static synchronized void somemethod_2() {
    // method code goes here
  }
}

我们可以声明一个实例方法和一个静态方法同步。构造函数不能声明为同步。

以下代码说明了使用关键字synchronized:

public class main {
  public synchronized void somemethod_1() {
    // only one thread can execute here at a time
  }

  public void somemethod_11() {
    synchronized (this) {
      // only one thread can execute here at a time
    }
  }

  public void somemethod_12() {
    // multiple threads can execute here at a time
    synchronized (this) {
      // only one thread can execute here at a time
    }
    // multiple threads can execute here at a time
  }

  public static synchronized void somemethod_2() {
    // only one thread can execute here at a time
  }

  public static void somemethod_21() {
    synchronized (main.class) {
      // only one thread can execute here at a time
    }
  }

  public static void somemethod_22() {
    // multiple threads can execute here at a time
    synchronized (main.class) {
      // only one thread can execute here at a time
    }
    // multiple threads can execute here at a time
  }
}

wait()方法

对wait()方法的调用必须放在synchronized方法或同步块中。

对于当前线程已经获取监视器的对象,必须调用wait()方法。

notify()方法

没有办法唤醒等待集中的特定线程。

例子

public class main {
  private static int myvalue = 1;

  public static void main(string[] args) {
    thread t = new thread(() -> {
      while (true) {
        updatebalance();
      }
    });
    t.start();
    t = new thread(() -> {
      while (true) {
        monitorbalance();
      }
    });
    t.start();
  }

  public static synchronized void updatebalance() {
    system.out.println("start:" + myvalue);
    myvalue = myvalue + 1;
    myvalue = myvalue - 1;
    system.out.println("end:" + myvalue);
  }

  public static synchronized void monitorbalance() {
    int b = myvalue;
    if (b != 1) {
      system.out.println("balance  changed: " + b);
      system.exit(1); 
    }
  }
}

上面的代码生成以下结果。

例2

以下代码显示了上述代码的非同步版本。

public class main {
  private static int myvalue = 1;

  public static void main(string[] args) {
    thread t = new thread(() -> {
      while (true) {
        updatebalance();
      }
    });
    t.start();
    t = new thread(() -> {
      while (true) {
        monitorbalance();
      }
    });
    t.start();
  }

  public static  void updatebalance() {
    system.out.println("start:" + myvalue);
    myvalue = myvalue + 1;
    myvalue = myvalue - 1;
    system.out.println("end:" + myvalue);
  }

  public static synchronized void monitorbalance() {
    int b = myvalue;
    if (b != 1) {
      system.out.println("balance  changed: " + b);
      system.exit(1); 
    }
  }
}

上面的代码生成以下结果。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册