Skip to content

Commit 636a350

Browse files
realDuYuanChaogithub-actions
andauthored
update thread (#150)
* update thread * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent d2f9f40 commit 636a350

18 files changed

Lines changed: 282 additions & 0 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.examplehub.basics.lambda;
2+
3+
public interface SumWithLambda {
4+
public int sum(int n);
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.basics.thread;
2+
3+
import java.util.concurrent.Callable;
4+
5+
public class CallableExample implements Callable<Integer> {
6+
7+
@Override
8+
public Integer call() throws Exception {
9+
int sum = 0;
10+
for (int i = 1; i <= 100; ++i) {
11+
sum += i;
12+
}
13+
return sum;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class DaemonThread implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
while (Thread.currentThread().getState() != Thread.State.TERMINATED) {
8+
System.out.println("daemon thread is terminated");
9+
}
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class JoinThread implements Runnable {
4+
@Override
5+
public void run() {
6+
for (int i = 0; i < 10; i++) {
7+
System.out.println(Thread.currentThread().getName() + i);
8+
}
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class PriorityOfThread implements Runnable {
4+
@Override
5+
public void run() {
6+
System.out.println(
7+
Thread.currentThread().getName() + "-> priority = " + Thread.currentThread().getPriority());
8+
}
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class StateOfThread implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
for (int i = 0; i < 5; i++) {
8+
try {
9+
Thread.sleep(1000);
10+
} catch (InterruptedException e) {
11+
e.printStackTrace();
12+
}
13+
}
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class StopThread implements Runnable {
4+
5+
private boolean flag = true;
6+
7+
@Override
8+
public synchronized void run() {
9+
while (true) {
10+
for (int i = 1; ; ++i) {
11+
System.out.println(i);
12+
try {
13+
Thread.sleep(500);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
}
19+
}
20+
21+
public void stop() {
22+
flag = false;
23+
System.out.println("sub thread stopped");
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class YieldThread implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
System.out.println(Thread.currentThread().getName() + " is running");
8+
Thread.yield();
9+
System.out.println(Thread.currentThread().getName() + " is stopped");
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
/** https://leetcode.com/problems/third-maximum-number/ */
7+
public class ThirdMaximumNumber {
8+
public static int solution1(int... nums) {
9+
HashSet<Integer> set = new HashSet<>();
10+
for (int num : nums) {
11+
set.add(num);
12+
}
13+
int i = 0;
14+
for (int num : set) {
15+
nums[i++] = num;
16+
}
17+
Arrays.sort(nums, 0, i);
18+
return i < 3 ? nums[i - 1] : nums[i - 3];
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.examplehub.basics.lambda;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SumWithLambdaTest {
8+
@Test
9+
void testLambda() {
10+
SumWithLambda sumWithLambda =
11+
n -> {
12+
int total = 0;
13+
for (int i = 1; i <= n; ++i) {
14+
total = total + i;
15+
}
16+
return total;
17+
};
18+
assertEquals(5050, sumWithLambda.sum(100));
19+
}
20+
}

0 commit comments

Comments
 (0)