update:demo10

This commit is contained in:
dichgrem
2026-01-13 21:24:59 +08:00
parent 6922192da4
commit 6ce5c76843
3 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package com.study.demo10;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{name='" + name + "', score=" + score + "}";
}
}
public class LambdaCollection {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 85));
students.add(new Student("李四", 92));
students.add(new Student("王五", 78));
System.out.println("排序前:");
students.forEach(s -> System.out.println(s));
Collections.sort(students, (s1, s2) -> s2.score - s1.score);
System.out.println("\n排序后按成绩降序");
students.forEach(s -> System.out.println(s));
}
}

View File

@@ -0,0 +1,21 @@
package com.study.demo10;
public class MultiTaskThread {
public static void main(String[] args) {
Runnable task1 = () -> {
for (int i = 0; i < 3; i++) {
System.out.println("任务一 - " + Thread.currentThread().getName());
}
};
Runnable task2 = () -> {
System.out.println("任务二 - " + Thread.currentThread().getName() + " - 正在处理业务...");
};
Thread thread1 = new Thread(task1, "线程A");
Thread thread2 = new Thread(task2, "线程B");
thread1.start();
thread2.start();
}
}

View File

@@ -0,0 +1,19 @@
package com.study.demo10;
public class TaskRunner {
public void runTask() {
class LocalTask {
void execute() {
System.out.println("正在执行临时任务");
}
}
LocalTask task = new LocalTask();
task.execute();
}
public static void main(String[] args) {
TaskRunner runner = new TaskRunner();
runner.runTask();
}
}