Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ public interface HttpEndpointRouter {

String route(List<String> endpoints);

// Load Balance
// Random
// RoundRibbon
// Weight
// - server01,20
// - server02,30
// - server03,50

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void run() {
};
Thread thread = new Thread(task);
thread.setName("test-thread-1");
thread.setDaemon(true);
thread.setDaemon(false);
thread.start();
}

Expand Down
4 changes: 3 additions & 1 deletion 03concurrency/0301/src/main/java/java0/conc0301/Runner2.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public void run() {
boolean result = Thread.currentThread().isInterrupted();

boolean result1 = Thread.interrupted(); // 重置状态

boolean result3 = Thread.currentThread().isInterrupted();

System.out.println("Runner2.run result ===>" + result);
System.out.println("Runner2.run result1 ===>" + result1);
System.out.println("Runner2.run result3 ===>" + result3);


}
}
14 changes: 9 additions & 5 deletions 03concurrency/0301/src/main/java/java0/conc0301/RunnerMain.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

package java0.conc0301;

import java.io.IOException;

public class RunnerMain {

public static void main(String[] args) {
public static void main(String[] args) throws IOException {

Runner1 runner1 = new Runner1();
Thread thread1 = new Thread(runner1);
Expand All @@ -14,12 +16,14 @@ public static void main(String[] args) {
thread1.start();
thread2.start();

thread2.interrupt();
thread2.interrupt(); // i = true

System.out.println(Thread.activeCount());

// Thread.currentThread().getThreadGroup().list();
// System.out.println(Thread.currentThread().getThreadGroup().getParent().activeGroupCount());
// Thread.currentThread().getThreadGroup().getParent().list();
Thread.currentThread().getThreadGroup().list();
System.out.println(Thread.currentThread().getThreadGroup().getParent().activeGroupCount());
Thread.currentThread().getThreadGroup().getParent().list();


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void run() {
String currentThreadName = currentThread.getName();

System.out.println("这是线程的名称:" + currentThreadName);
System.out.println("返回当前线程" + currentThreadName + "的线程组中活动线程的数量:" + Thread.activeCount());
System.out.println("返回当前线程" + currentThreadName + "的线程组中活动线程的数量:" + Thread.currentThread().getThreadGroup().activeCount());
System.out.println("返回该线程" + currentThreadName + "的标识符:" + currentThread.getId());
System.out.println("返回该线程" + currentThreadName + "的优先级:" + currentThread.getPriority());
System.out.println("返回该线程" + currentThreadName + "的状态:" + currentThread.getState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(String[] args) {
new Thread(threadB).start();
System.out.println("这是主线程:");

ThreadC threadC = new ThreadC();
ThreadC threadC = new ThreadC();
FutureTask<String> futureTask = new FutureTask<>(threadC);
new Thread(futureTask).start();
System.out.println("这是主线程:begin!");
Expand Down
4 changes: 2 additions & 2 deletions 03concurrency/0301/src/main/java/java0/conc0301/op/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void main(String[] args) {
thread1.setOo(oo);
thread1.start();

synchronized (oo) {
synchronized (thread1) {
for (int i = 0; i < 100; i++) {
if (i == 20) {
try {
Expand Down Expand Up @@ -40,7 +40,7 @@ public MyThread(String name) {

@Override
public void run() {
synchronized (oo) {
synchronized (this) {
for (int i = 0; i < 100; i++) {
System.out.println(name + i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public static void main(String[] args) {
e.printStackTrace();
}
}, "t2");
Thread t3 = new Thread(() -> {
try {
methodClass.customer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t3");
// Thread t3 = new Thread(() -> {
// try {
// methodClass.customer();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }, "t3");
t1.start();
t2.start();
t3.start();
//t3.start();

}
}
Expand Down
21 changes: 13 additions & 8 deletions 03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package java0.conc0301.sync;

public class Counter {
private int sum = 0;
public void incr() {
sum++;

public final static int A=10;

public static int B=10;

private volatile int sum = 0;
public synchronized void incr() {
sum=sum+1;
}
public int getSum() {
return sum;
}

public static void main(String[] args) throws InterruptedException {
int loop = 10000;
int loop = 100000;

// test single thread
Counter counter = new Counter();
Expand All @@ -33,10 +38,10 @@ public static void main(String[] args) throws InterruptedException {
});
t1.start();
t2.start();
//Thread.sleep(300);
while (Thread.activeCount()>2){//当前线程的线程组中的数量>2
Thread.yield();
}
Thread.sleep(1000);
// while (Thread.activeCount()>2){//当前线程的线程组中的数量>2
// Thread.yield();
// }
System.out.println("multiple threads: " + counter2.getSum());


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package java0.conc0301.sync;

public class Cref {
public static void main(String[] args) {
int x = 10;
int y = Counter.B;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

package java0.conc0302.atomic;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicCount {

private AtomicInteger num = new AtomicInteger();

public synchronized int add() {
return num.getAndIncrement();
}

public int getNum() {
return num.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

package java0.conc0302.atomic;

public class AtomicMain {

public static void main(String[] args) {
final AtomicCount count = new AtomicCount();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10000; j++) {
count.add();
}
}
}).start();
}

try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("num=" + count.getNum());
}

}
15 changes: 15 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/atomic/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package java0.conc0302.atomic;

public class Count {

private int num = 0;

public int add() {
return num++;
}

public int getNum() {
return num;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package java0.conc0302.atomic;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

public class LongDemo {

public static void main(String[] args) {

final AtomicLong atomicLong = new AtomicLong();
final LongAdder longAdder = new LongAdder();

for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10000; j++) {
atomicLong.getAndIncrement();
longAdder.increment();
}
}
}).start();
}

try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("atomicLong=" + atomicLong.get());
System.out.println("longAdder =" + longAdder.sum());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package java0.conc0302.atomic;

public class SyncCount {

private int num = 0;

public synchronized int add() {
return num++;
}

public int getNum() {
return num;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package java0.conc0302.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ConditionDemo {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();

final Object[] items = new Object[20];
int putptr, takeptr, count;

public void put(Object x) throws InterruptedException {
lock.lock();
try {
// 当count等于数组的大小时,当前线程等待,直到notFull通知,再进行生产
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}

public Object take() throws InterruptedException {
lock.lock();
try {
// 当count为0,进入等待,直到notEmpty通知,进行消费。
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package java0.conc0302.lock;

import java.util.concurrent.locks.LockSupport;

public class LockSupportDemo {

public static Object u = new Object();
static ChangeObjectThread t1 = new ChangeObjectThread("t1");
static ChangeObjectThread t2 = new ChangeObjectThread("t2");

public static class ChangeObjectThread extends Thread {
public ChangeObjectThread(String name) {
super(name);
}
@Override public void run() {
synchronized (u) {
System.out.println("in " + getName());
LockSupport.park();
if (Thread.currentThread().isInterrupted()) {
System.out.println("被中断了");
}
System.out.println("继续执行");
}
}
}

public static void main(String[] args) throws InterruptedException {
t1.start();
Thread.sleep(1000L);
t2.start();
Thread.sleep(3000L);
t1.interrupt();
LockSupport.unpark(t2);
t1.join();
t2.join();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package java0.conc0301.threadpool;
package java0.conc0302.threadpool;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package java0.conc0301.threadpool;
package java0.conc0302.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down
Loading