写点什么

Java 线程

作者:GalaxyCreater
  • 2022 年 7 月 31 日
  • 本文字数:572 字

    阅读完需:约 2 分钟

资料

https://www.w3cschool.cn/java/java-thread-producer-consumer.html


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()方法。

用户头像

GalaxyCreater

关注

还未添加个人签名 2019.04.21 加入

还未添加个人简介

评论

发布
暂无评论
Java线程_Java_GalaxyCreater_InfoQ写作社区