Skip to content

Commit d958d0b

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
update thread and fixed build failed (#148)
* update thread * fixed LGTM * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 03573f6 commit d958d0b

13 files changed

+164
-57
lines changed

.DS_Store

6 KB
Binary file not shown.

src/main/java/com/examplehub/basics/ThreadExample.java

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.examplehub.basics.io;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
11+
import java.nio.file.StandardCopyOption;
12+
13+
public class DownloadFile {
14+
public static boolean solution1(String url, String to) throws MalformedURLException {
15+
try (BufferedInputStream bufferedInputStream =
16+
new BufferedInputStream(new URL(url).openStream())) {
17+
BufferedOutputStream bufferedOutputStream =
18+
new BufferedOutputStream(new FileOutputStream(to));
19+
byte[] bytes = new byte[1024];
20+
int readBytes;
21+
while ((readBytes = bufferedInputStream.read(bytes)) != -1) {
22+
bufferedOutputStream.write(bytes, 0, readBytes);
23+
bufferedOutputStream.flush();
24+
}
25+
bufferedOutputStream.close();
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
return false;
29+
}
30+
return true;
31+
}
32+
33+
public static boolean solution2(String url, String to) throws IOException {
34+
BufferedInputStream bufferedInputStream = new BufferedInputStream(new URL(url).openStream());
35+
Files.copy(bufferedInputStream, Paths.get(to), StandardCopyOption.REPLACE_EXISTING);
36+
bufferedInputStream.close();
37+
return true;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class ExampleRunnable implements Runnable {
4+
5+
@Override
6+
public void run() {
7+
for (int i = 0; i < 10; ++i) {
8+
System.out.println(Thread.currentThread().getName() + "->" + i);
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class ExampleThread extends Thread {
4+
@Override
5+
public void run() {
6+
for (int i = 0; i < 10; ++i) {
7+
System.out.println(getName() + "->" + i);
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class SellTicketThread implements Runnable {
4+
5+
private int totalTickets = 10;
6+
7+
@Override
8+
public void run() {
9+
while (totalTickets > 0) {
10+
System.out.println(Thread.currentThread().getName() + " buy " + totalTickets + " nth ticket");
11+
totalTickets--;
12+
}
13+
}
14+
}

src/main/java/com/examplehub/domain/Student.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public Student(int id, int chinese, int math, int english) {
1515
this.english = english;
1616
}
1717

18+
public int getTotal() {
19+
return this.getChinese() + this.getMath() + this.getEnglish();
20+
}
21+
1822
public int getId() {
1923
return id;
2024
}
@@ -49,12 +53,14 @@ public void setEnglish(int english) {
4953

5054
@Override
5155
public int compareTo(Student o) {
52-
if (this.Chinese + this.math + this.english != o.getChinese() + o.getMath() + o.getMath()) {
53-
return o.getChinese() + o.getMath() + o.getMath() - this.Chinese - this.math - this.english;
54-
} else if (this.Chinese != o.getChinese()) {
55-
return o.getChinese() - this.Chinese;
56+
if (this.getTotal() != o.getTotal()) {
57+
return this.getTotal() - o.getTotal();
58+
} else if (this.getChinese() != o.getChinese()) {
59+
return this.getChinese() - o.getChinese();
60+
} else if (this.getMath() != o.getMath()) {
61+
return this.getMath() - o.getMath();
5662
} else {
57-
return o.getMath() - this.math;
63+
return this.getMath() - o.getMath();
5864
}
5965
}
6066

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.basics.io;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.io.IOException;
6+
import org.junit.jupiter.api.Test;
7+
8+
class DownloadFileTest {
9+
@Test
10+
void testSolution1() throws IOException {
11+
String url = "https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_100kB.jpg";
12+
String name = "images/file_example_1.jpg";
13+
assertTrue(DownloadFile.solution1(url, name));
14+
}
15+
16+
@Test
17+
void TestSolution2() throws IOException {
18+
String url = "https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_100kB.jpg";
19+
String name = "images/file_example_2.jpg";
20+
assertTrue(DownloadFile.solution2(url, name));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.basics.thread;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ExampleRunnableTest {
8+
@Test
9+
void testRunnable() {
10+
Thread thread = new Thread(new ExampleRunnable());
11+
thread.setName("runnable-thread");
12+
thread.start();
13+
for (int i = 0; i < 10; ++i) {
14+
System.out.println(Thread.currentThread().getName() + "->" + i);
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.basics.thread;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ExampleThreadTest {
8+
@Test
9+
void test() {
10+
ExampleThread exampleThread = new ExampleThread();
11+
exampleThread.setName("custom-thread");
12+
exampleThread.start();
13+
for (int i = 0; i < 10; ++i) {
14+
System.out.println(Thread.currentThread().getName() + "->" + i);
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.examplehub.basics.thread;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SellTicketThreadTest {
8+
@Test
9+
void test() {
10+
SellTicketThread sellTicketThread = new SellTicketThread();
11+
12+
Thread firstThread = new Thread(sellTicketThread);
13+
Thread secondThread = new Thread(sellTicketThread);
14+
Thread thirdThread = new Thread(sellTicketThread);
15+
16+
firstThread.start();
17+
secondThread.start();
18+
thirdThread.start();
19+
}
20+
}

src/test/java/com/examplehub/domain/StudentTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ class StudentTest {
99
void testComparable() {
1010
Student stuA = new Student(1, 99, 99, 99);
1111
Student stuB = new Student(1, 99, 99, 99);
12-
assertNotEquals(stuA, stuB);
1312
assertEquals(stuB.compareTo(stuA), 0);
1413
stuA = new Student(1, 100, 99, 99);
1514
stuB = new Student(1, 99, 99, 99);
16-
assertTrue(stuB.compareTo(stuA) > 0);
15+
assertTrue(stuA.compareTo(stuB) > 0);
1716
stuA = new Student(1, 100, 99, 99);
1817
stuB = new Student(1, 99, 100, 99);
19-
assertTrue(stuB.compareTo(stuA) > 0);
18+
assertTrue(stuA.compareTo(stuB) > 0);
2019
}
2120
}

src/test/java/com/examplehub/sorts/BubbleSortTest.java

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.examplehub.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
43
import static org.junit.jupiter.api.Assertions.assertTrue;
54

65
import com.examplehub.domain.Student;
@@ -54,21 +53,6 @@ void testComparable() {
5453
students[3] = new Student(4, 100, 100, 100);
5554
students[4] = new Student(5, 99, 99, 99);
5655
sort.sort(students);
57-
// sort by total score, chinese, math, english
58-
for (Student item : students) {
59-
System.out.format(
60-
"id:%d, totalScore:%d, Chinese:%d, math:%d, english:%d\n",
61-
item.getId(),
62-
item.getChinese() + item.getMath() + item.getEnglish(),
63-
item.getChinese(),
64-
item.getMath(),
65-
item.getEnglish());
66-
System.out.println(item);
67-
}
68-
assertEquals("Student{id=4, Chinese=100, math=100, english=100}", students[0].toString());
69-
assertEquals("Student{id=2, Chinese=100, math=99, english=98}", students[1].toString());
70-
assertEquals("Student{id=1, Chinese=98, math=99, english=100}", students[2].toString());
71-
assertEquals("Student{id=3, Chinese=100, math=98, english=99}", students[3].toString());
72-
assertEquals("Student{id=5, Chinese=99, math=99, english=99}", students[4].toString());
56+
assertTrue(SortUtils.isSorted(students));
7357
}
7458
}

0 commit comments

Comments
 (0)