Skip to content

Commit b334afa

Browse files
committed
update
1 parent d5ee0df commit b334afa

File tree

8 files changed

+262
-50
lines changed

8 files changed

+262
-50
lines changed

geektime_javaconcurrency/src/main/java/MatrixDemo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public static void main(String[] args) {
2323
}
2424
}
2525
}
26+
27+
2628
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package alograthim;
2+
3+
/**
4+
* @author XuYanXin
5+
* @program javaconcurrency_learn
6+
* @description
7+
* @date 2020/9/23 8:18 下午
8+
*/
9+
10+
/*
11+
public class Demo {
12+
*/
13+
/**
14+
* 分析:数据规模是不确定的所以不能忽略,而是要相加 首先要循环 n 次,所以 O(n)
15+
* 另外循环进行判断 pos == x 最好情况是第一次就找到,最坏是最后找到,也就是又循环了 n 次
16+
* O(1) ~ O(n) ,按最坏来 O(n+n) O(2n) 忽略常数 O(n)
17+
*//*
18+
19+
int find(int[] array, int n, int x) {
20+
int i = 0;
21+
int pos = -1;
22+
for (; i < n; ++i) {
23+
if (array[i] == x) {
24+
pos = i;
25+
break;
26+
}
27+
}
28+
return pos;
29+
}
30+
31+
// array 代表一个长度为 n 的数组, 方法中的 array.lenght 就是数据规模 n
32+
int[] array = new int[n];
33+
int count = 0;
34+
35+
void insert(int val) {
36+
if (count == array.length) {
37+
int sum = 0;
38+
for (int i = 0; i < array.length; ++i) {
39+
sum = sum + array[i];
40+
}
41+
array[0] = sum;
42+
count = 1;
43+
}
44+
array[count] = val;
45+
++count;
46+
}
47+
}
48+
*/
Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
1-
//package chapter1;
2-
//
3-
//
4-
//import lombok.extern.slf4j.Slf4j;
5-
//
6-
///**
7-
// * @author XuYanXin
8-
// * @program javaconcurrency_learn
9-
// * @description
10-
// * @date 2020/4/9 2:15 下午
11-
// */
12-
//
13-
//@Slf4j
14-
//public class Test {
15-
// private long count = 0;
16-
//
17-
// private void add10K() {
18-
// int idx = 0;
19-
// while (idx++ < 100000000) {
20-
// count += 1;
21-
// }
22-
// log.info("add10k()方法执行完毕时Count的值: ---> {}",count);
23-
// }
24-
//
25-
// public static long calc() throws InterruptedException {
26-
// final Test test = new Test();
27-
// Thread th1 = new Thread(test::add10K);
28-
//
29-
// Thread th2 = new Thread(test::add10K);
30-
//
31-
// // 启动 th1,th2 线程
32-
// th1.start();
33-
// th2.start();
34-
// // 等待两个线程执行结束 th1.join(); th2.join();
35-
// th1.join();
36-
// th2.join();
37-
// return test.count;
38-
// }
39-
//
40-
// public static void main(String[] args) throws InterruptedException {
41-
// for (int i = 0; i < 10; i++) {
42-
// long calc = Test.calc();
43-
// log.info("本次返回结果{}",calc);
44-
// }
45-
// }
46-
//}
47-
//
48-
//
1+
package chapter1;
2+
3+
4+
import java.util.concurrent.atomic.AtomicLong;
5+
import lombok.extern.slf4j.Slf4j;
6+
7+
/**
8+
* @author XuYanXin
9+
* @program javaconcurrency_learn
10+
* @description
11+
* @date 2020/4/9 2:15 下午
12+
*/
13+
14+
@Slf4j
15+
public class Test {
16+
private static AtomicLong atomicCount = new AtomicLong(0);
17+
private long count = 0;
18+
19+
private void add10K() {
20+
int idx = 0;
21+
while (idx++ < 10000) {
22+
count += 1;
23+
atomicCount.incrementAndGet();
24+
}
25+
}
26+
27+
public static long calc() throws InterruptedException {
28+
final Test test = new Test();
29+
// 创建两个线程,执行 add10K 方法
30+
Thread th1 = new Thread(test::add10K);
31+
Thread th2 = new Thread(test::add10K);
32+
33+
// 启动 th1,th2 线程
34+
th1.start();
35+
th2.start();
36+
// 等待两个线程执行结束 th1.join(); th2.join();
37+
th1.join();
38+
th2.join();
39+
return test.count;
40+
}
41+
42+
public static void main(String[] args) throws InterruptedException {
43+
for (int i = 0; i < 10; i++) {
44+
long calc = Test.calc();
45+
log.info("本次 count 结果{}",calc);
46+
log.info("本次原子类返回结果{}",atomicCount.longValue());
47+
System.out.println("\r\n");
48+
atomicCount = new AtomicLong(0);
49+
}
50+
}
51+
}
52+
53+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter3;
2+
3+
/**
4+
* @author XuYanXin
5+
* @program javaconcurrency_learn
6+
* @description
7+
* @date 2020/9/18 4:32 下午
8+
*/
9+
10+
public class Test {
11+
public static void test() {
12+
int i = 10;
13+
while (i < 20);
14+
15+
System.out.println("66666");
16+
}
17+
18+
public static void main(String[] args) {
19+
test();
20+
}
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.example.cleancode;
2+
3+
/**
4+
* @author XuYanXin
5+
* @program javaconcurrency_learn
6+
* @description
7+
* @date 2020/9/19 1:46 上午
8+
*/
9+
10+
/*
11+
public class Square {
12+
public Point topLeft;
13+
public double side;
14+
}
15+
16+
public class Rectangle {
17+
public Point topLeft;
18+
public double height;
19+
public double width;
20+
}
21+
22+
public class Circle {
23+
public Point center;
24+
public double radius;
25+
}
26+
27+
public class Geometry {
28+
public final double PI = 3.141592653589793;
29+
30+
public double area(Object shape) throws NoSuchShapeException {
31+
if (shape instanceof Square) {
32+
Square s = (Square) shape;
33+
return s.side * s.side;
34+
} else if (shape instanceof Rectangle) {
35+
Rectangle r = (Rectangle) shape;
36+
return r.height * r.width;
37+
} else if (shape instanceof Circle) {
38+
Circle c = (Circle) shape;
39+
return PI * c.radius * c.radius;
40+
}
41+
throw new NoSuchShapeException;
42+
}
43+
}
44+
*/

java_conccurency_practice/src/main/java/leetcode/LeetCode.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.HashMap;
5+
import java.util.HashSet;
56
import java.util.LinkedList;
67
import java.util.Queue;
78

@@ -433,6 +434,7 @@ private static boolean binarySearch(int[][] martrix, int target, int start, bool
433434
* ],
434435
* k = 8,
435436
*
437+
*
436438
* 返回 13。
437439
* 提示: 可以假设 k 的值永远是有效的 1 <= k <= n 的平方
438440
*
@@ -457,6 +459,55 @@ public static int kthSmallest(int[][] matrix, int k) {
457459
return r;
458460
}
459461

462+
/**
463+
* 287. 寻找重复数
464+
* 中等难度
465+
*
466+
* 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n)
467+
* 可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
468+
*
469+
* 示例: 输入 [1,3,4,2,2]
470+
* 输出 2
471+
*
472+
* 输入 [3,1,3,4,2]
473+
* 输出 3
474+
*
475+
* 说明:
476+
*
477+
* 不能更改原数组(假设数组是只读的)。
478+
* 只能使用额外的 O(1) 的空间。
479+
* 时间复杂度小于 O(n^2) 。
480+
* 数组中只有一个重复的数字,但它可能不止重复出现一次。
481+
*
482+
* 思路:
483+
* 遍历数组,并将元素存入 Set 并调用 contain 元素的操作,如果返回 true 则返回当前元素
484+
*
485+
* @param nums
486+
* @return
487+
*/
488+
public static int findDuplicate(int[] nums) {
489+
if (nums == null || nums.length == 0) {
490+
return 0;
491+
}
492+
// 创建一个 Set 来保存元素
493+
HashSet<Integer> elements = new HashSet<>(nums.length);
494+
495+
for (int i = 0; i < nums.length; i++) {
496+
497+
if (i == 0) {
498+
elements.add(nums[i]);
499+
continue;
500+
}
501+
if (elements.contains(nums[i])) {
502+
return nums[i];
503+
} else {
504+
elements.add(nums[i]);
505+
}
506+
}
507+
return -1;
508+
}
509+
510+
460511
private static int countNoMoreThanMid(int[][] matrix, int mid, int n) {
461512
int x = n, y = 0, count = 0;
462513
while (x >= 0 && y <= n) {
@@ -520,8 +571,12 @@ public static void main(String[] args) {
520571
System.out.println(searchMatrixBinary(arr, 5));*/
521572

522573
// 378
523-
int[][] arr = {{1, 5,9}, {10, 11,13},{12,13,15}};
524-
System.out.println(kthSmallest(arr, 8));
574+
//int[][] arr = {{1, 5, 9}, {10, 11, 13}, {12, 13, 15}};
575+
//System.out.println(kthSmallest(arr, 8));
576+
577+
//int[] test287arr = {3,1,3,4,2};
578+
int[] test287arr = {1, 2, 2};
579+
System.out.println(findDuplicate(test287arr));
525580
}
526581

527582
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package yunaispring;
2+
3+
/**
4+
* @author XuYanXin
5+
* @program javaconcurrency_learn
6+
* @description
7+
* @date 2020/11/6 3:24 下午
8+
*/
9+
10+
public class BeautifulGirl {
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package yunaispring;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.Random;
6+
7+
/**
8+
* @author XuYanXin
9+
* @program javaconcurrency_learn
10+
* @description
11+
* @date 2020/11/7 6:10 下午
12+
*/
13+
14+
public class Test {
15+
public static void main(String[] args) {
16+
ArrayList<Integer> source = new ArrayList<>(101);
17+
HashMap<Integer, Integer> map = new HashMap<>(100);
18+
for (int i = 1; i <= 101; i++) {
19+
source.add(i);
20+
if (i <= 101 - 5) {
21+
map.put(i, i + 5);
22+
}
23+
}
24+
System.out.println(map);
25+
}
26+
}

0 commit comments

Comments
 (0)