Download Thread_project/Thread_module/src/Thread_safe_package/MyRunnable.java from zxy0307/Java: direct link, hf CLI and curl.
- Browser
- Download file 789 Bytes
-
https://huggingface.co/zxy0307/Java/resolve/main/Thread_project/Thread_module/src/Thread_safe_package/MyRunnable.java
- Command line
-
hf download hf://zxy0307/Java/Thread_project/Thread_module/src/Thread_safe_package/MyRunnable.java
-
curl -L -o MyRunnable.java https://huggingface.co/zxy0307/Java/resolve/main/Thread_project/Thread_module/src/Thread_safe_package/MyRunnable.java
789 Bytes
| package Thread_safe_package; | |
| public class MyRunnable implements Runnable { | |
| static int ticket = 0; | |
| public void run() { | |
| //1、循环 | |
| while (true) { | |
| //2、同步代码块(同步方法) | |
| if (method()) break; | |
| } | |
| } | |
| private synchronized boolean method() { | |
| //3、判断共享数据是否到了末尾 | |
| if (ticket == 100) { | |
| return true; | |
| } else { | |
| try { | |
| Thread.sleep(10); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| ticket++; | |
| System.out.println(Thread.currentThread().getName() + | |
| "在卖第" + ticket + "张票"); | |
| } | |
| return false; | |
| } | |
| } | |