Skip to content

Commit 080905f

Browse files
authored
Merge pull request #2 from JavaCourse00/main
update
2 parents 832bfb0 + 68af61f commit 080905f

13 files changed

Lines changed: 108 additions & 50 deletions

File tree

02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public static void main(String[] args) throws InterruptedException {
2222
try {
2323
ServerBootstrap b = new ServerBootstrap();
2424
b.option(ChannelOption.SO_BACKLOG, 128)
25-
.option(ChannelOption.TCP_NODELAY, true)
26-
.option(ChannelOption.SO_KEEPALIVE, true)
27-
.option(ChannelOption.SO_REUSEADDR, true)
28-
.option(ChannelOption.SO_RCVBUF, 32 * 1024)
29-
.option(ChannelOption.SO_SNDBUF, 32 * 1024)
30-
.option(EpollChannelOption.SO_REUSEPORT, true)
25+
.childOption(ChannelOption.TCP_NODELAY, true)
3126
.childOption(ChannelOption.SO_KEEPALIVE, true)
32-
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
27+
.childOption(ChannelOption.SO_REUSEADDR, true)
28+
.childOption(ChannelOption.SO_RCVBUF, 32 * 1024)
29+
.childOption(ChannelOption.SO_SNDBUF, 32 * 1024)
30+
.childOption(EpollChannelOption.SO_REUSEPORT, true)
31+
.childOption(ChannelOption.SO_KEEPALIVE, true)
32+
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
3333

3434
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
3535
.handler(new LoggingHandler(LogLevel.INFO))

02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundServer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public void run() throws Exception {
3636
try {
3737
ServerBootstrap b = new ServerBootstrap();
3838
b.option(ChannelOption.SO_BACKLOG, 128)
39-
.option(ChannelOption.TCP_NODELAY, true)
40-
.option(ChannelOption.SO_KEEPALIVE, true)
41-
.option(ChannelOption.SO_REUSEADDR, true)
42-
.option(ChannelOption.SO_RCVBUF, 32 * 1024)
43-
.option(ChannelOption.SO_SNDBUF, 32 * 1024)
44-
.option(EpollChannelOption.SO_REUSEPORT, true)
39+
.childOption(ChannelOption.TCP_NODELAY, true)
4540
.childOption(ChannelOption.SO_KEEPALIVE, true)
46-
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
41+
.childOption(ChannelOption.SO_REUSEADDR, true)
42+
.childOption(ChannelOption.SO_RCVBUF, 32 * 1024)
43+
.childOption(ChannelOption.SO_SNDBUF, 32 * 1024)
44+
.childOption(EpollChannelOption.SO_REUSEPORT, true)
45+
.childOption(ChannelOption.SO_KEEPALIVE, true)
46+
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
4747

4848
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
4949
.handler(new LoggingHandler(LogLevel.DEBUG))

03concurrency/0301/src/main/java/java0/conc0301/DaemonThread.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33
public class DaemonThread {
44

5-
public static void main(String[] args) {
6-
Runnable task = new Runnable() {
7-
@Override
8-
public void run() {
5+
public static void main(String[] args) throws InterruptedException {
6+
Runnable task = () -> {
97
try {
10-
Thread.sleep(5000);
11-
} catch (InterruptedException e) {
12-
e.printStackTrace();
13-
}
8+
Thread.sleep(1000);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
1412
Thread t = Thread.currentThread();
1513
System.out.println("当前线程:" + t.getName());
16-
}
1714
};
1815
Thread thread = new Thread(task);
1916
thread.setName("test-thread-1");
20-
thread.setDaemon(false);
17+
thread.setDaemon(true);
2118
thread.start();
19+
20+
//Thread.sleep(2000);
2221
}
2322

2423

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package java0.conc0301;
2+
3+
public class ThreadCount {
4+
public static void main(String[] args) throws InterruptedException {
5+
//System.out.println("system:"+Thread.currentThread().getThreadGroup().getParent());
6+
Thread.currentThread().getThreadGroup().getParent().list();
7+
8+
// System.out.println("main:"+Thread.currentThread().getThreadGroup());
9+
// Thread.currentThread().getThreadGroup().list();
10+
}
11+
}

03concurrency/0301/src/main/java/java0/conc0301/op/Join.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ public static void main(String[] args) {
66
Object oo = new Object();
77

88
MyThread thread1 = new MyThread("thread1 -- ");
9+
//oo = thread1;
910
thread1.setOo(oo);
1011
thread1.start();
1112

12-
synchronized (thread1) {
13+
synchronized (oo) { // 这里用oo或thread1/this
1314
for (int i = 0; i < 100; i++) {
1415
if (i == 20) {
1516
try {
16-
thread1.join();
17+
oo.wait(0);
18+
//thread1.join();
1719
} catch (InterruptedException e) {
1820
e.printStackTrace();
1921
}
@@ -40,7 +42,7 @@ public MyThread(String name) {
4042

4143
@Override
4244
public void run() {
43-
synchronized (this) {
45+
synchronized (oo) { // 这里用oo或this,效果不同
4446
for (int i = 0; i < 100; i++) {
4547
System.out.println(name + i);
4648
}

03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Counter {
77
public static int B=10;
88

99
private volatile int sum = 0;
10-
public synchronized void incr() {
10+
public void incr() {
1111
sum=sum+1;
1212
}
1313
public int getSum() {

03concurrency/0301/src/main/java/java0/conc0302/atomic/AtomicMain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
public class AtomicMain {
55

66
public static void main(String[] args) {
7-
final AtomicCount count = new AtomicCount();
7+
final SyncCount count = new SyncCount();
88
for (int i = 0; i < 100; i++) {
99
new Thread(new Runnable() {
1010
@Override
@@ -17,7 +17,7 @@ public void run() {
1717
}
1818

1919
try {
20-
Thread.sleep(1000L);
20+
Thread.sleep(5000L);
2121
} catch (InterruptedException e) {
2222
e.printStackTrace();
2323
}

03concurrency/0301/src/main/java/java0/conc0302/atomic/SyncCount.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11

22
package java0.conc0302.atomic;
33

4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
47
public class SyncCount {
58

69
private int num = 0;
710

8-
public synchronized int add() {
9-
return num++;
11+
private Lock lock = new ReentrantLock(true);
12+
13+
public int add() {
14+
try {
15+
lock.lock();
16+
return num++;
17+
} finally {
18+
lock.unlock();
19+
}
1020
}
1121

1222
public int getNum() {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package java0.conc0302.lock;
2+
3+
import java.util.concurrent.atomic.AtomicReference;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
public class TestFair {
7+
public static volatile int race=0;
8+
public static ReentrantLock lock = new ReentrantLock(true); // 改成false会好100倍
9+
public static void increase(){
10+
lock.lock();
11+
race++; //变量自增操作
12+
lock.unlock();
13+
}
14+
private static final int THREADS_COUNT=20;
15+
public static void main(String[]args){
16+
int count = Thread.activeCount();
17+
long now = System.currentTimeMillis();
18+
System.out.println(count);
19+
AtomicReference<Thread> sign =new AtomicReference<>();
20+
Thread[]threads=new Thread[THREADS_COUNT]; //定义20个线程
21+
for(int i=0;i<THREADS_COUNT;i++){
22+
threads[i]=new Thread(new Runnable(){
23+
@Override
24+
public void run(){
25+
for(int i=0;i<100000;i++){
26+
increase();
27+
}
28+
}
29+
});
30+
threads[i].start();
31+
}//等待所有累加线程都结束
32+
while(Thread.activeCount()>count) {
33+
Thread.yield();
34+
}
35+
System.out.println(lock.getClass().getName() + " ts = "+ (System.currentTimeMillis()-now));
36+
}
37+
}
38+

03concurrency/0301/src/main/java/java0/conc0302/threadpool/ExceptionDemo.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@ public static void main(String[] args) {
1111

1212
try {
1313
Future<Double> future = executorService.submit(() -> {
14-
int a = 1;
15-
return 10.0/(a-1);
14+
throw new RuntimeException("executorService.submit()");
1615
});
1716

1817
double b = future.get();
1918
System.out.println(b);
2019

2120
} catch (Exception ex) {
22-
System.out.println("catch execute");
21+
System.out.println("catch submit");
2322
ex.printStackTrace();
2423
}
25-
26-
try {
27-
executorService.execute(() -> {
28-
int a = 1;
29-
float b = 10/(a-1);
30-
});
31-
} catch (Exception ex) {
32-
System.out.println("catch execute");
33-
ex.printStackTrace();
34-
}
35-
24+
//
25+
// try {
26+
// executorService.execute(() -> {
27+
// throw new RuntimeException("executorService.execute()");
28+
// });
29+
// } catch (Exception ex) {
30+
// System.out.println("catch execute");
31+
// ex.printStackTrace();
32+
// }
33+
//
3634
executorService.shutdown();
3735
System.out.println("Main Thread End!");
3836
}

0 commit comments

Comments
 (0)