dengxinyi 6 年之前
父节点
当前提交
4acffd88a2
共有 4 个文件被更改,包括 99 次插入1 次删除
  1. 31 0
      oj/concurrent/TestExecutor.java
  2. 41 0
      oj/concurrent/TestInterrupt.java
  3. 9 1
      oj/concurrent/TestRunnable.java
  4. 18 0
      oj/concurrent/TestThread.java

+ 31 - 0
oj/concurrent/TestExecutor.java

@@ -0,0 +1,31 @@
+import java.util.concurrent.*;
+
+public class TestExecutor {
+    public static void main(String[] args) {
+        Thread daemon = new Thread(new MyDaemon());
+        daemon.setDaemon(true);
+        daemon.start();
+        // ExecutorService executor = Executors.newCachedThreadPool();
+        // ExecutorService executor = Executors.newSingleThreadExecutor();
+        ExecutorService executor = Executors.newFixedThreadPool(2);
+        for (int i = 0; i < 5; i++) {
+            executor.execute(new MyRunnable(i));
+        }
+        executor.shutdown();
+    }
+}
+
+class MyDaemon implements Runnable {
+    @Override
+    public void run() {
+        while (true) {
+            Thread.yield();
+            try {
+                Thread.sleep(1000);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            System.out.println("Daemon is running.");
+        }
+    }
+}

+ 41 - 0
oj/concurrent/TestInterrupt.java

@@ -0,0 +1,41 @@
+import java.util.concurrent.*;
+
+public class TestInterrupt {
+    public static void main(String[] args) {
+        InterruptThread thread = new InterruptThread();
+        thread.start();
+        try {
+            Thread.sleep(500);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        thread.interrupt();
+
+        ExecutorService executor = Executors.newFixedThreadPool(1);
+        executor.execute(() -> {
+            System.out.println("Thread start!");
+            try {
+                Thread.sleep(1000);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+        executor.shutdownNow();
+    }
+}
+
+class InterruptThread extends Thread {
+    @Override
+    public void run() {
+        while (!interrupted()) {
+            System.out.println("Thread is running.");
+            try {
+                Thread.sleep(1000);
+            } catch (Exception e) {
+                e.printStackTrace();
+                return;
+            }
+        }
+        System.out.println("Thread end.");
+    }
+}

+ 9 - 1
oj/concurrent/TestRunnable.java

@@ -7,6 +7,14 @@ public class TestRunnable {
 }
 
 class MyRunnable implements Runnable {
+    int id = -1;
+
+    public MyRunnable() {}
+
+    public MyRunnable(int id) {
+        this.id = id;
+    }
+
     @Override
     public void run() {
         try {
@@ -14,6 +22,6 @@ class MyRunnable implements Runnable {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        System.out.println("Finished.");
+        System.out.println(id == -1 ? "Finished." : ("Thread " + id + " finished."));
     }
 }

+ 18 - 0
oj/concurrent/TestThread.java

@@ -0,0 +1,18 @@
+public class TestThread {
+    public static void main(String[] args) {
+        MyThread thread = new MyThread();
+        thread.start();
+    }
+}
+
+class MyThread extends Thread {
+    @Override
+    public void run() {
+        try {
+            Thread.sleep(1000);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        System.out.println("Finished.");
+    }
+}