邓心一 4 سال پیش
والد
کامیت
283d4e8e13
2فایلهای تغییر یافته به همراه7 افزوده شده و 4 حذف شده
  1. 1 0
      basis/concurrent/TestSemaphore.java
  2. 6 4
      basis/concurrent/TestWaitAndNotify.java

+ 1 - 0
basis/concurrent/TestSemaphore.java

@@ -12,6 +12,7 @@ public class TestSemaphore {
                     semaphore.acquire();
                     System.out.println("Thread " + num + " start.");
                     Thread.sleep(100);
+                    System.out.println("Thread " + num + " end.");
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {

+ 6 - 4
basis/concurrent/TestWaitAndNotify.java

@@ -1,7 +1,7 @@
 public class TestWaitAndNotify {
     public static void main(String[] args) {
-        WaitThread t1 = new WaitThread(1, 2, 50, false);
-        WaitThread t2 = new WaitThread(2, 2, 50, false);
+        WaitThread t1 = new WaitThread("t1", 1, 2, 50, false);
+        WaitThread t2 = new WaitThread("t2", 2, 2, 50, false);
         t1.start();
         t2.start();
     }
@@ -11,12 +11,14 @@ class MyLock {}
 
 class WaitThread extends Thread {
     private static MyLock lock = new MyLock();
+    private String name;
     private int init;
     private int delta;
     private int count;
     private boolean isLast;
 
-    WaitThread(int init, int delta, int count, boolean isLast) {
+    WaitThread(String name, int init, int delta, int count, boolean isLast) {
+        this.name = name;
         this.init = init;
         this.delta = delta;
         this.count = count;
@@ -26,7 +28,7 @@ class WaitThread extends Thread {
     @Override
     public void run() {
         for (int i = 0, num = init; i < count; i++) {
-            System.out.println(num);
+            System.out.println(name + ": " + num);
             num += delta;
             if (isLast && i == count - 1) return;
             synchronized (lock) {