public class SellTicket {
public static void main(String[] args) {
SellTicke01 sellTicke01 = new SellTicke01();
SellTicke01 sellTicke02 = new SellTicke01();
SellTicke01 sellTicke03 = new SellTicke01();
Thread thread1 = new Thread(sellTicke01);
Thread thread2 = new Thread(sellTicke02);
Thread thread3 = new Thread(sellTicke03);
thread1.start();
thread2.start();
thread3.start();
}
}
class SellTicke01 implements Runnable {
static boolean loop = true;
static int ticketSum = 0;
static public synchronized void sell() {
// for (int i = 0; i < 100; i++) {
if (ticketSum == 30000) {
loop = false;
return;
}
System.out.println(Thread.currentThread().getName() + " " + (++ticketSum));
// try {
// Thread.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
@Override
public void run() {
while (loop) {
sell();
// try {
// Thread.sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
}
Java中一个类的对象的线程调用了synchronzed修饰的方法以后,期间这个线程被sleep了,那原来被synchrnzed修饰的方法就可以被其他的同一个类的对象的另一个线程调用了吧?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- CSDN专家-sinJack 2022-09-16 22:52关注
是的。当一个线程获取锁资源的时候,其他线程就处于等待中,直到同步方法执行完毕后,才继续抢占资源。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
- ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
- ¥15 MATLAB中streamslice问题
- ¥15 如何在炒股软件中,爬到我想看的日k线
- ¥15 seatunnel 怎么配置Elasticsearch
- ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
- ¥15 (标签-MATLAB|关键词-多址)
- ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
- ¥500 52810做蓝牙接受端
- ¥15 基于PLC的三轴机械手程序