diff --git a/BasicSyntax1.kt b/BasicSyntax1.kt new file mode 100644 index 0000000..1fd389c --- /dev/null +++ b/BasicSyntax1.kt @@ -0,0 +1,32 @@ +class BasicSyntax1 { + companion object { + @JvmStatic + fun main(args: Array) { + val getBook = ::Book + println(getBook.name) + + val bookNames = listOf( + Book("Thinking in Java"), + Book("啊啊") + ) + bookNames.map { b -> println(b.name) } + + bookNames.map(Book::name).map { s -> println(s) } + println(bookNames) + + val a = { x: Int -> x + 1 } + println(a(1)) + + var b = 1 + fun foo() = if (b > 0) { + b = 2 + b + } else 0 + + println(foo()) + } + } +} + + +class Book(val name: String) diff --git a/jTest/ConcurrentToolsExample.java b/jTest/ConcurrentToolsExample.java new file mode 100644 index 0000000..bd34335 --- /dev/null +++ b/jTest/ConcurrentToolsExample.java @@ -0,0 +1,42 @@ +package jTest; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.Phaser; + +public class ConcurrentToolsExample { + public static void main(String[] args) throws InterruptedException, BrokenBarrierException { + CyclicBarrier cyclicBarrier = new CyclicBarrier(2); + CountDownLatch countDownLatch = new CountDownLatch(1); + Phaser phaser = new Phaser(1); // 初始注册线程数为1 + + new Thread(() -> { + try { + cyclicBarrier.await(); // 等待 + // 执行任务 + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + + new Thread(() -> { + try { + countDownLatch.await(); // 等待 + // 执行任务 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }).start(); + + // 到达并等待 + // 执行任务 + new Thread(phaser::arriveAndAwaitAdvance).start(); + + // 模拟一些前置操作 + Thread.sleep(1000); + countDownLatch.countDown(); // 计数减一,允许等待的线程继续执行 + cyclicBarrier.await(); // 释放等待的线程 + phaser.register(); // 增加一个参与者 + } +} \ No newline at end of file diff --git a/jTest/ThreadPoolExample.java b/jTest/ThreadPoolExample.java new file mode 100644 index 0000000..c1bc5dd --- /dev/null +++ b/jTest/ThreadPoolExample.java @@ -0,0 +1,23 @@ +package jTest; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class ThreadPoolExample { + public static void main(String[] args) throws InterruptedException { + // 创建一个固定大小的线程池 + ExecutorService executor = Executors.newFixedThreadPool(4); + + // 提交任务到线程池 + for (int i = 0; i < 10; i++) { + // Thread.sleep(1000); + // System.out.println("Running task " + Thread.currentThread().getName()); + executor.submit(() -> { + System.out.println("Running task " + Thread.currentThread().getName()); + }); + } + + // 关闭线程池 + executor.shutdown(); + } +} \ No newline at end of file diff --git a/jTest/ThreadTest1.java b/jTest/ThreadTest1.java new file mode 100644 index 0000000..a3cd172 --- /dev/null +++ b/jTest/ThreadTest1.java @@ -0,0 +1,24 @@ +package jTest; + +public class ThreadTest1 { + public static void main(String[] args) { + Runnable r = () -> System.out.println(1); + + // r.run(); + + try { + Thread thread = new Thread(r); + System.out.println(thread.getState()); + Thread.yield(); + System.out.println(thread.getState()); + Thread.sleep(1000); + System.out.println(thread.getState()); + thread.join(); + System.out.println(thread.getState()); + thread.start(); + System.out.println(thread.getState()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/jTest/ThreadTest2.java b/jTest/ThreadTest2.java new file mode 100644 index 0000000..407d56f --- /dev/null +++ b/jTest/ThreadTest2.java @@ -0,0 +1,20 @@ +package jTest; + +public class ThreadTest2 { + // 1 + // 2 + // 66666 + // 3 + public static void main(String[] args) { + System.out.println(1); + + Runnable runnable = () -> { + System.out.println("66666"); + }; + + System.out.println(2); + runnable.run(); + + System.out.println(3); + } +} diff --git a/jTest/ThreadTest3.java b/jTest/ThreadTest3.java new file mode 100644 index 0000000..97b77c8 --- /dev/null +++ b/jTest/ThreadTest3.java @@ -0,0 +1,18 @@ +package jTest; + +public class ThreadTest3 { + // 第一步 + // 第二步 + // 第三步 + // 第四步 + // 1 + public static void main(String[] args) { + System.out.println("第一步"); + Runnable r = () -> System.out.println(1); + System.out.println("第二步"); + Thread thread = new Thread(r); + System.out.println("第三步"); + thread.start(); + System.out.println("第四步"); + } +} diff --git a/jTest/ThreadTest4.java b/jTest/ThreadTest4.java new file mode 100644 index 0000000..65d6b9d --- /dev/null +++ b/jTest/ThreadTest4.java @@ -0,0 +1,31 @@ +package jTest; + +public class ThreadTest4 { + // 线程中的内容 + // 线程2 Thread-2 正在运行 + // 线程1 Thread-1 正在运行 + public static void main(String[] args) { + Thread thread = new Thread(() -> { + System.out.println("线程中的内容"); + }); + + // 使用start()方法来启动线程,它会调用run()方法。不要直接调用run()方法,因为这不会创建新线程。 + new MyThread().start(); + thread.start(); + new Thread(new MyRunnable()).start(); + } +} + +class MyThread extends Thread { + public void run() { + // 线程要执行的代码 + System.out.println("线程1 " + Thread.currentThread().getName() + " 正在运行"); + } +} + +class MyRunnable implements Runnable { + public void run() { + // 线程要执行的代码 + System.out.println("线程2 " + Thread.currentThread().getName() + " 正在运行"); + } +} diff --git a/jTest/ThreadTestWait.java b/jTest/ThreadTestWait.java new file mode 100644 index 0000000..7ff1a1a --- /dev/null +++ b/jTest/ThreadTestWait.java @@ -0,0 +1,76 @@ +package jTest; + +public class ThreadTestWait { + public static void main(String[] args) { + // 使用 + SharedResource resource = new SharedResource(); + new Producer(resource).start(); + new Consumer(resource).start(); + } +} + +class SharedResource { + private final Object lock = new Object(); + private boolean ready = false; + + public void produce() throws InterruptedException { + synchronized (lock) { + while (ready) { + lock.wait(); // 等待直到消费者消费 + } + // 生产资源 + ready = true; + lock.notifyAll(); // 通知所有等待的线程 + } + } + + public void consume() throws InterruptedException { + synchronized (lock) { + while (!ready) { + lock.wait(); // 等待直到生产者生产 + } + // 消费资源 + ready = false; + lock.notifyAll(); // 通知所有等待的线程 + } + } +} + +class Producer extends Thread { + private final SharedResource resource; + + public Producer(SharedResource resource) { + this.resource = resource; + } + + public void run() { + try { + while (true) { + resource.produce(); + Thread.sleep(1000); // 模拟生产时间 + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} + + +class Consumer extends Thread { + private final SharedResource resource; + + public Consumer(SharedResource resource) { + this.resource = resource; + } + + public void run() { + try { + while (true) { + resource.consume(); + Thread.sleep(1000); // 模拟消费时间 + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} \ No newline at end of file diff --git a/out/production/Kotlin-Demo/.gitignore b/out/production/Kotlin-Demo/.gitignore new file mode 100644 index 0000000..ff93b79 --- /dev/null +++ b/out/production/Kotlin-Demo/.gitignore @@ -0,0 +1,32 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +logs/ +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ + +### VS Code ### +.vscode/ diff --git a/out/production/Kotlin-Demo/BasicSyntax1$Companion$main$a$1.class b/out/production/Kotlin-Demo/BasicSyntax1$Companion$main$a$1.class new file mode 100644 index 0000000..6e491ac Binary files /dev/null and b/out/production/Kotlin-Demo/BasicSyntax1$Companion$main$a$1.class differ diff --git a/out/production/Kotlin-Demo/BasicSyntax1$Companion$main$getBook$1.class b/out/production/Kotlin-Demo/BasicSyntax1$Companion$main$getBook$1.class new file mode 100644 index 0000000..f126a70 Binary files /dev/null and b/out/production/Kotlin-Demo/BasicSyntax1$Companion$main$getBook$1.class differ diff --git a/out/production/Kotlin-Demo/BasicSyntax1$Companion.class b/out/production/Kotlin-Demo/BasicSyntax1$Companion.class new file mode 100644 index 0000000..2b75de3 Binary files /dev/null and b/out/production/Kotlin-Demo/BasicSyntax1$Companion.class differ diff --git a/out/production/Kotlin-Demo/BasicSyntax1.class b/out/production/Kotlin-Demo/BasicSyntax1.class new file mode 100644 index 0000000..a8f4264 Binary files /dev/null and b/out/production/Kotlin-Demo/BasicSyntax1.class differ diff --git a/out/production/Kotlin-Demo/Book.class b/out/production/Kotlin-Demo/Book.class new file mode 100644 index 0000000..49a2d15 Binary files /dev/null and b/out/production/Kotlin-Demo/Book.class differ diff --git a/out/production/Kotlin-Demo/jTest/ConcurrentToolsExample.class b/out/production/Kotlin-Demo/jTest/ConcurrentToolsExample.class new file mode 100644 index 0000000..767542f Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ConcurrentToolsExample.class differ diff --git a/out/production/Kotlin-Demo/jTest/Consumer.class b/out/production/Kotlin-Demo/jTest/Consumer.class new file mode 100644 index 0000000..86e2ae2 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/Consumer.class differ diff --git a/out/production/Kotlin-Demo/jTest/MyRunnable.class b/out/production/Kotlin-Demo/jTest/MyRunnable.class new file mode 100644 index 0000000..0a07ffe Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/MyRunnable.class differ diff --git a/out/production/Kotlin-Demo/jTest/MyThread.class b/out/production/Kotlin-Demo/jTest/MyThread.class new file mode 100644 index 0000000..eb186c1 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/MyThread.class differ diff --git a/out/production/Kotlin-Demo/jTest/Producer.class b/out/production/Kotlin-Demo/jTest/Producer.class new file mode 100644 index 0000000..6dd7ced Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/Producer.class differ diff --git a/out/production/Kotlin-Demo/jTest/SharedResource.class b/out/production/Kotlin-Demo/jTest/SharedResource.class new file mode 100644 index 0000000..0089ec7 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/SharedResource.class differ diff --git a/out/production/Kotlin-Demo/jTest/ThreadPoolExample.class b/out/production/Kotlin-Demo/jTest/ThreadPoolExample.class new file mode 100644 index 0000000..ea63d90 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ThreadPoolExample.class differ diff --git a/out/production/Kotlin-Demo/jTest/ThreadTest1.class b/out/production/Kotlin-Demo/jTest/ThreadTest1.class new file mode 100644 index 0000000..b638f9f Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ThreadTest1.class differ diff --git a/out/production/Kotlin-Demo/jTest/ThreadTest2.class b/out/production/Kotlin-Demo/jTest/ThreadTest2.class new file mode 100644 index 0000000..80cc7a0 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ThreadTest2.class differ diff --git a/out/production/Kotlin-Demo/jTest/ThreadTest3.class b/out/production/Kotlin-Demo/jTest/ThreadTest3.class new file mode 100644 index 0000000..2e19983 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ThreadTest3.class differ diff --git a/out/production/Kotlin-Demo/jTest/ThreadTest4.class b/out/production/Kotlin-Demo/jTest/ThreadTest4.class new file mode 100644 index 0000000..37b7842 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ThreadTest4.class differ diff --git a/out/production/Kotlin-Demo/jTest/ThreadTestWait.class b/out/production/Kotlin-Demo/jTest/ThreadTestWait.class new file mode 100644 index 0000000..50b0ef4 Binary files /dev/null and b/out/production/Kotlin-Demo/jTest/ThreadTestWait.class differ diff --git a/out/production/Kotlin-Demo/test1/BasicSyntax.class b/out/production/Kotlin-Demo/test1/BasicSyntax.class index 04cffd4..c608deb 100644 Binary files a/out/production/Kotlin-Demo/test1/BasicSyntax.class and b/out/production/Kotlin-Demo/test1/BasicSyntax.class differ diff --git a/out/production/Kotlin-Demo/test1/BasicSyntaxKt.class b/out/production/Kotlin-Demo/test1/BasicSyntaxKt.class index d8cb008..bfd86d3 100644 Binary files a/out/production/Kotlin-Demo/test1/BasicSyntaxKt.class and b/out/production/Kotlin-Demo/test1/BasicSyntaxKt.class differ