线程相关

This commit is contained in:
Bunny 2024-08-18 02:30:03 +08:00
parent 490199fd8a
commit 1f919fffba
28 changed files with 298 additions and 0 deletions

32
BasicSyntax1.kt Normal file
View File

@ -0,0 +1,32 @@
class BasicSyntax1 {
companion object {
@JvmStatic
fun main(args: Array<String>) {
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)

View File

@ -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(); // 增加一个参与者
}
}

View File

@ -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();
}
}

24
jTest/ThreadTest1.java Normal file
View File

@ -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);
}
}
}

20
jTest/ThreadTest2.java Normal file
View File

@ -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);
}
}

18
jTest/ThreadTest3.java Normal file
View File

@ -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("第四步");
}
}

31
jTest/ThreadTest4.java Normal file
View File

@ -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() + " 正在运行");
}
}

76
jTest/ThreadTestWait.java Normal file
View File

@ -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();
}
}
}

32
out/production/Kotlin-Demo/.gitignore vendored Normal file
View File

@ -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/

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.