Skip to content

Commit 9779027

Browse files
committed
Java
1 parent 53c0493 commit 9779027

6 files changed

Lines changed: 122 additions & 44 deletions

File tree

‎docs/AimForOffer/9_回溯.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ private boolean valid(int threshlod ,int x,int y){
151151

152152
private int walk(int threshold,int startx,int starty){
153153
visited[startx][starty]=true;
154-
int walks = 1;
154+
int walks = 0;
155+
if(valid(threshold,startx,starty)){
156+
walks=1;
157+
}
155158
for(int i=0;i<4;i++){
156159
int newX = startx+d[i][0];
157160
int newY = starty+d[i][1];

‎docs/Java_Concurrency/1_基础知识.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ sleep() 和 wait() 的区别:
389389

390390
sleep() 是 Thread 的静态方法;
391391

392-
wait() 是 Object 的常量方法,所以任何对象都可以调用 wait()
392+
wait() 是 Object 的成员方法,所以任何对象都可以调用 wait()
393393

394394
- 锁是否会释放
395395

@@ -409,6 +409,10 @@ Thread.yield() 声明了当前线程已经完成了生命周期中最重要的
409409

410410
yield() 和 wait() 的区别:
411411

412+
- yield() 是 Thread 的静态方法
413+
414+
wait() 是 Object 的成员方法
415+
412416
- yield() 让当前线程由“运行状态”进入“就绪状态”;
413417

414418
wait() 让当前线程由“运行状态”进入“等待状态”
@@ -417,6 +421,16 @@ yield() 和 wait() 的区别:
417421

418422
wait() 会释放同步锁,使得其他线程可以使用同步块或者同步方法
419423

424+
### notify() 和 notifyAll()
425+
426+
锁池:线程 A 已经拥有某个对象的锁,线程 B、C 想要调用这个对象的同步方法,此时线程 B、C 就会被阻塞,进入一个地方去等待锁的释放,这个地方就是该对象的锁池。
427+
428+
等待池:假设线程 A 调用了某个对象的 wait() 方法,线程 A 释放该对象的锁,同时线程 A 进入等待池,进入等待池的线程不会竞争该对象的锁。
429+
430+
**notifyAll() 让所有处于等待池的线程进入锁池去竞争锁**。
431+
432+
433+
420434
## 五、终止线程的几种方式
421435

422436
### 1. 退出标志

‎docs/Java_Concurrency/2_并发理论.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static void main(String[] args) throws InterruptedException {
130130

131131
- volatile,保证被 volatile 修饰的共享变量对所有线程总是可见的
132132
- synchronized,**对一个变量执行 unlock 操作之前,必须把变量值同步回主内存**。
133-
- final,被 final 关键字修饰的字段在构造器中一旦初始化完成,并且没有发生 this 逃逸(其它线程通过 this 引用访问到初始化了一半的对象),那么其它线程就能看见 final 字段的值。
133+
- final,被 final 关键字修饰的字段在构造器中一旦初始化完成,并且没有发生 this 逃逸(其它线程通过 this 引用访问到未初始化完成的对象),那么其它线程就能看见 final 字段的值。
134134

135135
注意: volatile 并不能保证操作的原子性。
136136

‎docs/Java_Concurrency/3_并发关键字.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ volatile 有 2 个主要功能:
3232
**线程安全问题**:
3333

3434
- 存在共享数据(临界资源)
35-
- 存在多条线程共同操作这些共享数据操作共享数据
35+
- 存在多条线程共同操作这些共享数据
3636

3737
解决:**同步机制**。
3838

‎docs/Java_Concurrency/6_并发容器.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,28 @@ Hashtable 全表锁
250250

251251
ConcurrentHashMap 分段锁
252252

253-
![img](https://gitee.com/duhouan/ImagePro/raw/master/java-notes/java/16_00.jpg)
253+
![img](https://gitee.com/duhouan/ImagePro/raw/master/java-notes/java/16_00.jpg)
254+
255+
# 补充
256+
257+
## 红黑树性质
258+
259+
1. 每个节点或者是红色的,或者是黑色的
260+
261+
2. 根节点是黑色的
262+
263+
3. 每一个叶子节点(最后的空节点)是黑色的
264+
265+
> 这其实是一个定义
266+
267+
4. 如果一个节点是红色的,那么它们的孩子节点都是黑色的
268+
269+
> 因为红节点和其父亲节点表示一个3节点,黑色节点的右孩子一定是黑色节点。
270+
271+
5. 从任意一个节点到叶子节点经过的**黑色节点**都是一样的
272+
273+
> 因为2-3树是绝对平衡的
274+
275+
![img](https://gitee.com/duhouan/ImagePro/raw/master/java-notes/dataStructure/redBlackTree//12_11.png)
276+
277+
相较于 AVL 树,**红黑树上的查找的操作的效率的低于 AVL 树的。但对于增删操作,红黑树的性能是更优的。**

‎docs/Java_Concurrency/7_并发工具.md‎

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,52 @@ Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的
9696

9797
```java
9898
public class SemaphoreExample {
99-
10099
public static void main(String[] args) {
101-
final int clientCount = 3;
102-
final int totalRequestCount = 10;
103-
Semaphore semaphore = new Semaphore(clientCount);
104-
ExecutorService executorService = Executors.newCachedThreadPool();
105-
for (int i = 0; i < totalRequestCount; i++) {
106-
executorService.execute(()->{
107-
try {
108-
semaphore.acquire();
109-
System.out.print(semaphore.availablePermits() + " ");
110-
} catch (InterruptedException e) {
111-
e.printStackTrace();
112-
} finally {
113-
semaphore.release();
100+
final int threadNum=1;
101+
final int totalThread=10;
102+
103+
Semaphore semaphore=new Semaphore(threadNum);
104+
105+
ExecutorService service= Executors.newCachedThreadPool();
106+
107+
for(int i=0;i<totalThread;i++){
108+
final int num=i;
109+
service.execute(new Runnable() {
110+
@Override
111+
public void run() {
112+
try {
113+
semaphore.acquire();
114+
test(num);
115+
} catch (InterruptedException e) {
116+
e.printStackTrace();
117+
}finally {
118+
semaphore.release();
119+
}
114120
}
115121
});
116122
}
117-
executorService.shutdown();
123+
service.shutdown();
124+
}
125+
126+
private static void test(int i) throws InterruptedException {
127+
System.out.println("Thread: "+i);
128+
Thread.sleep(1000);
118129
}
119130
}
120131
```
121132

122133
```html
123-
2 1 2 2 2 2 2 1 2 2
134+
// 每隔一秒钟出现一次
135+
Thread: 0
136+
Thread: 1
137+
Thread: 2
138+
Thread: 3
139+
Thread: 4
140+
Thread: 5
141+
Thread: 7
142+
Thread: 6
143+
Thread: 8
144+
Thread: 9
124145
```
125146

126147
### 4. CountDownLatch 和 CyclicBarrier 比较
@@ -217,51 +238,67 @@ java.util.concurrent.BlockingQueue 接口有以下阻塞队列的实现:
217238

218239
```java
219240
public class ProducerConsumer {
241+
private static BlockingQueue<String> queue=new LinkedBlockingQueue<>();
220242

221-
private static BlockingQueue<String> queue = new ArrayBlockingQueue<>(5);
222-
223-
private static class Producer extends Thread {
243+
private static class Producer extends Thread{
224244
@Override
225245
public void run() {
226246
try {
227247
queue.put("product");
248+
System.out.println("produce...");
228249
} catch (InterruptedException e) {
229250
e.printStackTrace();
230251
}
231-
System.out.print("produce..");
232252
}
233253
}
234254

235-
private static class Consumer extends Thread {
236-
255+
private static class Consumer extends Thread{
237256
@Override
238257
public void run() {
239258
try {
240-
String product = queue.take();
259+
queue.take();
260+
System.out.println("consume...");
241261
} catch (InterruptedException e) {
242262
e.printStackTrace();
243263
}
244-
System.out.print("consume..");
245264
}
246265
}
247-
}Copy to clipboardErrorCopied
248-
public static void main(String[] args) {
249-
for (int i = 0; i < 2; i++) {
250-
Producer producer = new Producer();
251-
producer.start();
252-
}
253-
for (int i = 0; i < 5; i++) {
254-
Consumer consumer = new Consumer();
255-
consumer.start();
256-
}
257-
for (int i = 0; i < 3; i++) {
258-
Producer producer = new Producer();
259-
producer.start();
266+
267+
public static void main(String[] args) {
268+
for(int i=0;i<2;i++){
269+
Producer p=new Producer();
270+
p.start();
271+
}
272+
273+
for(int i=0;i<5;i++){
274+
Consumer c=new Consumer();
275+
c.start();
276+
}
277+
278+
for(int i=0;i<3;i++){
279+
Producer p=new Producer();
280+
p.start();
281+
}
282+
260283
}
261-
}Copy to clipboardErrorCopied
262-
produce..produce..consume..consume..produce..consume..produce..consume..produce..consume..Copy to clipboardErrorCopied
284+
}
285+
```
286+
287+
```html
288+
produce...
289+
produce...
290+
consume...
291+
consume...
292+
produce...
293+
produce...
294+
consume...
295+
consume...
296+
produce...
297+
consume...
263298
```
264299

300+
301+
265302
### 3. ForkJoin
266303

267304
主要用于并行计算中,和 MapReduce 原理类似,都是把大的计算任务拆分成多个小任务并行计算。

0 commit comments

Comments
 (0)