mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 00:01:57 -05:00
update:demo10
This commit is contained in:
37
com/study/demo10/LambdaCollection.java
Normal file
37
com/study/demo10/LambdaCollection.java
Normal 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));
|
||||
}
|
||||
}
|
||||
21
com/study/demo10/MultiTaskThread.java
Normal file
21
com/study/demo10/MultiTaskThread.java
Normal 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();
|
||||
}
|
||||
}
|
||||
19
com/study/demo10/TaskRunner.java
Normal file
19
com/study/demo10/TaskRunner.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user