Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8f924b

Browse files
committed
style: optimize code
1 parent feef27e commit e8f924b

File tree

17 files changed

+556
-37
lines changed

17 files changed

+556
-37
lines changed

‎00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_349/kotlin/Solution.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package com.hi.dhl.algorithms.leetcode._349.kotlin
99
*/
1010

1111
class Solution {
12+
1213
fun intersection(nums1: IntArray, nums2: IntArray): IntArray {
1314
val set1 = HashSet<Int>();
1415
for (value in nums1) {
@@ -30,4 +31,4 @@ class Solution {
3031

3132
return result;
3233
}
33-
}
34+
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1114/Foo.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,39 @@ public void third(Runnable printThird) throws InterruptedException {
4747
sa.release();
4848
}
4949
}
50+
51+
// 测试
52+
public static void main(String... args) {
53+
Foo foo = new Foo();
54+
Thread tha = new Thread(new Runnable() {
55+
@Override
56+
public void run() {
57+
try {
58+
foo.first(() -> System.out.print("first"));
59+
} catch (Exception e) {
60+
61+
}
62+
}
63+
});
64+
65+
Thread thb = new Thread(() -> {
66+
try {
67+
foo.second(() -> System.out.print("second"));
68+
} catch (Exception e) {
69+
70+
}
71+
});
72+
73+
Thread thc = new Thread(() -> {
74+
try {
75+
foo.third(() -> System.out.print("third"));
76+
} catch (Exception e) {
77+
e.printStackTrace();
78+
}
79+
});
80+
81+
tha.start();
82+
thb.start();
83+
thc.start();
84+
}
5085
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1115/FooBar.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,26 @@ public void bar(Runnable printBar) throws InterruptedException {
4040
}
4141

4242
}
43+
44+
public static void main(String... args) {
45+
FooBar fooBar = new FooBar(20);
46+
Thread tha = new Thread(() -> {
47+
try {
48+
fooBar.foo(() -> System.out.print("foo"));
49+
} catch (Exception e) {
50+
51+
}
52+
});
53+
54+
Thread thb = new Thread(() -> {
55+
try {
56+
fooBar.bar(() -> System.out.print("bar"));
57+
} catch (Exception e) {
58+
59+
}
60+
});
61+
62+
tha.start();
63+
thb.start();
64+
}
4365
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1115/FooBar2.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,25 @@ public void bar(Runnable printBar) throws InterruptedException {
5050

5151
}
5252
}
53+
54+
public static void main(String... args) {
55+
FooBar2 fooBar = new FooBar2(10);
56+
Thread tha = new Thread(() -> {
57+
try {
58+
fooBar.foo(() -> System.out.print("foo"));
59+
} catch (Exception e) {
60+
61+
}
62+
});
63+
64+
Thread thb = new Thread(() -> {
65+
try {
66+
fooBar.bar(() -> System.out.print("bar"));
67+
} catch (Exception e) {
68+
69+
}
70+
});
71+
tha.start();
72+
thb.start();
73+
}
5374
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1116/ZeroEvenOddForCondition.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* </pre>
1414
*/
1515

16-
class ZeroEvenOddForCondition {
16+
class ZeroEvenOdd {
1717
private int n;
1818
private Lock lock = new ReentrantLock();
1919
private Condition czero = lock.newCondition();
@@ -22,7 +22,7 @@ class ZeroEvenOddForCondition {
2222
private int value = 0;
2323
private boolean zero = true;
2424

25-
public ZeroEvenOddForCondition(int n) {
25+
public ZeroEvenOdd(int n) {
2626
this.n = n;
2727
}
2828

@@ -79,4 +79,34 @@ public void odd(IntConsumer printNumber) throws InterruptedException {
7979
lock.unlock();
8080
}
8181
}
82+
83+
public static void main(String... args) {
84+
ZeroEvenOdd condition = new ZeroEvenOdd(10);
85+
Thread tha = new Thread(() -> {
86+
try {
87+
condition.zero(value -> System.out.print(value));
88+
} catch (Exception e) {
89+
90+
}
91+
});
92+
93+
Thread thb = new Thread(() -> {
94+
try {
95+
condition.even(value -> System.out.print(value));
96+
} catch (Exception e) {
97+
98+
}
99+
});
100+
101+
Thread thc = new Thread(() -> {
102+
try {
103+
condition.odd(value -> System.out.print(value));
104+
} catch (Exception e) {
105+
106+
}
107+
});
108+
tha.start();
109+
thb.start();
110+
thc.start();
111+
}
82112
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1116/ZeroEvenOddForSemaphore.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,60 @@ public ZeroEvenOddForSemaphore(int n) {
2222

2323
// printNumber.accept(x) outputs "x", where x is an integer.
2424
public void zero(IntConsumer printNumber) throws InterruptedException {
25-
for(int i=1;i<=n;i++){
25+
for(int i = 1; i <= n; i++){
2626
szero.acquire();
2727
printNumber.accept(0);
28-
if((i & 1) == 1){
28+
if((i & 1) == 1){
2929
sodd.release();
30-
}else{
30+
}else{
3131
seven.release();
3232
}
3333
}
3434
}
3535

3636
public void even(IntConsumer printNumber) throws InterruptedException {
37-
for(int i = 2; i<=n; i+=2){
37+
for(int i = 2; i <= n; i += 2) {
3838
seven.acquire();
3939
printNumber.accept(i);
4040
szero.release();
4141
}
4242
}
4343

4444
public void odd(IntConsumer printNumber) throws InterruptedException {
45-
for(int i = 1; i<=n; i+=2){
45+
for(int i = 1; i <= n; i += 2) {
4646
sodd.acquire();
4747
printNumber.accept(i);
4848
szero.release();
4949
}
5050
}
51+
52+
public static void main(String... args) {
53+
ZeroEvenOddForSemaphore semaphore = new ZeroEvenOddForSemaphore(10);
54+
Thread tha = new Thread(() -> {
55+
try {
56+
semaphore.zero(value -> System.out.print(value));
57+
} catch (Exception e) {
58+
59+
}
60+
});
61+
62+
Thread thb = new Thread(() -> {
63+
try {
64+
semaphore.even(value -> System.out.print(value));
65+
} catch (Exception e) {
66+
67+
}
68+
});
69+
70+
Thread thc = new Thread(() -> {
71+
try {
72+
semaphore.odd(value -> System.out.print(value));
73+
} catch (Exception e) {
74+
75+
}
76+
});
77+
tha.start();
78+
thb.start();
79+
thc.start();
80+
}
5181
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1116/ZeroEvenOddForWait.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,34 @@ public void odd(IntConsumer printNumber) throws InterruptedException {
5959
}
6060
}
6161
}
62+
63+
public static void main(String... args) {
64+
ZeroEvenOddForWait wait = new ZeroEvenOddForWait(10);
65+
Thread tha = new Thread(() -> {
66+
try {
67+
wait.zero(value -> System.out.print(value));
68+
} catch (Exception e) {
69+
70+
}
71+
});
72+
73+
Thread thb = new Thread(() -> {
74+
try {
75+
wait.even(value -> System.out.print(value));
76+
} catch (Exception e) {
77+
78+
}
79+
});
80+
81+
Thread thc = new Thread(() -> {
82+
try {
83+
wait.odd(value -> System.out.print(value));
84+
} catch (Exception e) {
85+
86+
}
87+
});
88+
tha.start();
89+
thb.start();
90+
thc.start();
91+
}
6292
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1117/H2O.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,41 @@ public void oxygen(Runnable releaseOxygen) throws InterruptedException {
5050

5151
}
5252
}
53+
54+
public static void main(String... agrs) {
55+
H2O h2o = new H2O();
56+
Thread tha1 = new Thread(() -> {
57+
try {
58+
h2o.hydrogen(() -> {
59+
System.out.print("H");
60+
});
61+
} catch (Exception e) {
62+
63+
}
64+
});
65+
Thread tha2 = new Thread(() -> {
66+
try {
67+
h2o.hydrogen(() -> {
68+
System.out.print("H");
69+
});
70+
} catch (Exception e) {
71+
72+
}
73+
});
74+
75+
Thread thb = new Thread(() -> {
76+
try {
77+
h2o.oxygen(() -> System.out.print("O"));
78+
} catch (Exception e) {
79+
80+
}
81+
});
82+
83+
// 2个氢线程
84+
tha1.start();
85+
tha2.start();
86+
87+
// 1个氧线程
88+
thb.start();
89+
}
5390
}

‎00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1117/H2O1.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,35 @@ public void oxygen(Runnable releaseOxygen) throws InterruptedException {
3131
releaseOxygen.run();
3232
sh.release(2);
3333
}
34+
35+
public static void main(String... agrs) {
36+
H2O1 h2o = new H2O1();
37+
Thread th1 = new Thread(() -> {
38+
try {
39+
h2o.hydrogen(() -> System.out.print("H"));
40+
} catch (Exception e) {
41+
42+
}
43+
});
44+
45+
Thread th2 = new Thread(() -> {
46+
try {
47+
h2o.hydrogen(() -> System.out.print("H"));
48+
} catch (Exception e) {
49+
50+
}
51+
});
52+
53+
Thread thb = new Thread(() -> {
54+
try {
55+
h2o.oxygen(() -> System.out.print("O"));
56+
} catch (Exception e) {
57+
58+
}
59+
});
60+
61+
th1.start();
62+
th2.start();
63+
thb.start();
64+
}
3465
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /