• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java 多线程

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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); 
    }
  }
}

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java TCP服务器发布时间:2022-01-22
下一篇:
JSON 语法发布时间:2022-01-22
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap