Skip to content

Commit eda37b3

Browse files
author
weilongzhao
committed
java code
0 parents  commit eda37b3

12 files changed

+428
-0
lines changed

BlockQueueDemo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.BlockingQueue;
5+
6+
public class BlockQueueDemo {
7+
public static void main(String[] args) {
8+
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
9+
10+
System.out.println(blockingQueue.offer("a"));
11+
System.out.println(blockingQueue.offer("a"));
12+
System.out.println(blockingQueue.offer("a"));
13+
System.out.println("====================");
14+
System.out.println(blockingQueue.element());
15+
System.out.println(blockingQueue.offer("a"));
16+
17+
18+
}
19+
20+
}

CallableDemo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.*;
4+
5+
/**
6+
* @author:weilongzhao
7+
* @time:2021/7/15 16:22
8+
*
9+
* 多线程已经有extants Thread 和Runnable 接口,为什么需要Callable 接口
10+
*/
11+
class MyThread implements Callable<Integer>{
12+
13+
@Override
14+
public Integer call() throws Exception {
15+
System.out.println(Thread.currentThread().getName()+" come in");
16+
TimeUnit.SECONDS.sleep(2);
17+
return 1024;
18+
}
19+
}
20+
public class CallableDemo {
21+
public static void main(String[] args) throws ExecutionException, InterruptedException {
22+
FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread());
23+
new Thread(futureTask,"AA").start();
24+
System.out.println(Thread.currentThread().getName()+" come in");
25+
System.out.println(futureTask.get()+100);
26+
}
27+
}

CountDownLatchDemo.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
public class CountDownLatchDemo {
6+
//前五个线程做完后,主线程才能执行
7+
//如何保证是期望的线程完成后,(cpu存在很多其它线程),因为在对应的线程结束后调用cdl.countDown();
8+
//主线程上没有写cdl.await() 会发生什么?->主线程可能先实行
9+
public static void main(String[] args) throws InterruptedException {
10+
CountDownLatch cdl = new CountDownLatch(5);
11+
for(int i = 0;i<5;i++){
12+
final int tmpInt = i;
13+
new Thread(()->{
14+
System.out.println(tmpInt+"is over");
15+
cdl.countDown();
16+
},String.valueOf(i)).start();
17+
}
18+
cdl.await();
19+
System.out.println("all is over");
20+
}
21+
}

CyclicBarrierDemo.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
6+
class CyclicBarrierDemo {
7+
public static void main(String[] args) {
8+
CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
9+
System.out.println("now start");
10+
});
11+
for(int i = 0;i<7;i++){
12+
final int tmpInt = i;
13+
new Thread(()->{
14+
System.out.println(tmpInt+"is over");
15+
try {
16+
cyclicBarrier.await();
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
} catch (BrokenBarrierException e) {
20+
e.printStackTrace();
21+
}
22+
},String.valueOf(i)).start();
23+
}
24+
}
25+
26+
}

Leecode.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
/**
4+
* @author:weilongzhao
5+
* @time:2021/7/15 21:38
6+
*/
7+
public class Leecode {
8+
public static void main(String[] args) {
9+
int[] arr = {1,7,3,6,5,6};
10+
//System.out.println(arrayCenterIndex(arr));
11+
int[] array = {0,1,2,2,3,3,3,4,4,4,4};
12+
System.out.println(deleteDuplicatedElements(array));;
13+
}
14+
15+
private static int arrayCenterIndex(int[] arr) {
16+
int total = 0;
17+
int sum = 0;
18+
for(int i = 0;i<arr.length;i++){
19+
sum += arr[i];
20+
}
21+
for(int i = 0;i<arr.length;i++){
22+
total += arr[i];
23+
if(total == sum){
24+
return i;
25+
}
26+
sum -= arr[i];
27+
}
28+
return -1;
29+
}
30+
public static int deleteDuplicatedElements(int[] array){
31+
int len = array.length;
32+
int count = 1;
33+
int j = 0;//慢游标
34+
for (int i = 1;i<len;i++,j++){
35+
if(array[i]!=array[j]){
36+
count++;
37+
}
38+
}
39+
return count;
40+
}
41+
}

ProConsumer_BlockQueueDemo.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
/**
8+
* @author:weilongzhao
9+
* @time:2021/7/13 18:57
10+
*/
11+
12+
class ShareData{
13+
private int number = 0;
14+
private Lock lock = new ReentrantLock();
15+
private Condition condition = lock.newCondition();
16+
public void increasement(){
17+
lock.lock();
18+
try {
19+
//判断
20+
while(number!=0){
21+
condition.await();
22+
}
23+
//干活
24+
number++;
25+
System.out.println(Thread.currentThread().getName()+"\t"+number);
26+
//通知
27+
condition.signalAll();
28+
}catch (InterruptedException e){
29+
e.printStackTrace();
30+
}finally {
31+
lock.unlock();
32+
}
33+
}
34+
public void decreasement(){
35+
lock.lock();
36+
try {
37+
//判断
38+
while(number==0){
39+
condition.await();
40+
}
41+
//干活
42+
number--;
43+
System.out.println(Thread.currentThread().getName()+"\t"+number);
44+
//通知
45+
condition.signalAll();
46+
}catch (InterruptedException e){
47+
e.printStackTrace();
48+
}finally {
49+
lock.unlock();
50+
}
51+
52+
}
53+
}
54+
public class ProConsumer_BlockQueueDemo {
55+
public static void main(String[] args) {
56+
ShareData shareData = new ShareData();
57+
new Thread(()->{
58+
for (int i = 0; i < 5; i++) {
59+
shareData.increasement();
60+
}
61+
},"A").start();
62+
new Thread(()->{
63+
for (int i = 0; i < 5; i++) {
64+
shareData.decreasement();
65+
}
66+
},"B").start();
67+
}
68+
}

ReadWriteLockDemo.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.locks.ReentrantReadWriteLock;
7+
8+
public class ReadWriteLockDemo {
9+
private Map map = new HashMap();
10+
private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
11+
12+
public void put(String key,Object value){
13+
rwLock.writeLock().lock();
14+
try {
15+
System.out.println(Thread.currentThread().getName()+"\t is writing:"+key);
16+
try{
17+
TimeUnit.MILLISECONDS.sleep(300);
18+
}catch (InterruptedException e){
19+
e.printStackTrace();
20+
}
21+
map.put(key,value);
22+
System.out.println(Thread.currentThread().getName()+"writing is over");
23+
}catch (Exception e){
24+
e.printStackTrace();
25+
}finally {
26+
rwLock.writeLock().unlock();
27+
}
28+
}
29+
public void get(String key){
30+
rwLock.readLock().lock();
31+
try{
32+
System.out.println(Thread.currentThread().getName()+"\t正在读取");
33+
try{
34+
TimeUnit.MILLISECONDS.sleep(300);
35+
}catch (InterruptedException e){
36+
e.printStackTrace();
37+
}
38+
Object result = map.get(key);
39+
System.out.println(Thread.currentThread().getName()+"\t读取完成");
40+
}catch (Exception e){
41+
e.printStackTrace();
42+
}finally {
43+
rwLock.readLock().lock();
44+
}
45+
}
46+
47+
public void getWithoutLock(String key){
48+
System.out.println(Thread.currentThread().getName()+"\tis reading");
49+
try{
50+
TimeUnit.MILLISECONDS.sleep(300);
51+
}catch (InterruptedException e){
52+
e.printStackTrace();
53+
}
54+
Object result = map.get(key);
55+
System.out.println(Thread.currentThread().getName()+"\treading is over");
56+
}
57+
58+
public static void main(String[] args) {
59+
ReadWriteLockDemo rwLockDemo = new ReadWriteLockDemo();
60+
for(int i = 0;i<5;i++){
61+
final int tmpInt = i;
62+
new Thread(()->{
63+
rwLockDemo.put("" + tmpInt, tmpInt);
64+
},String.valueOf(i)).start();
65+
}
66+
for(int i = 0;i<5;i++){
67+
final int tmpInt = i;
68+
//“使用final方法的原因有两个。第一个原因是把方法锁定,以防任何继承类修改它的含义;第二个原因是效率。在早期的Java实现版本中,会将final
69+
// 方法转为内嵌调用。但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升。在最近的Java版本中,不需要使用final方法进行这些优化了。“
70+
//对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象
71+
new Thread(()->{
72+
rwLockDemo.get(""+tmpInt);
73+
},String.valueOf(i)).start();
74+
}
75+
}
76+
}

SemaphoreDemo.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.Semaphore;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public class SemaphoreDemo {
7+
public static void main(String[] args) {
8+
Semaphore semaphore = new Semaphore(3);
9+
for(int i = 0;i<5;i++){
10+
final int tmpInt = i;
11+
new Thread(()->{
12+
try {
13+
semaphore.acquire();
14+
System.out.println(tmpInt+"is in");
15+
TimeUnit.SECONDS.sleep(3);
16+
semaphore.release();
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
}).start();
21+
}
22+
23+
}
24+
}

SingleInstanceDemo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
public class SingleInstanceDemo {
4+
private static SingleInstanceDemo sInstance = null;
5+
private SingleInstanceDemo (){
6+
System.out.println("sInstance is created");
7+
}
8+
public synchronized static SingleInstanceDemo get(){
9+
if (sInstance == null){
10+
sInstance = new SingleInstanceDemo();
11+
}
12+
return sInstance;
13+
}
14+
}

SpinLockDemo.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package edu.nwpu.zhao.Test;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.atomic.AtomicReference;
5+
6+
public class SpinLockDemo {
7+
AtomicReference<Thread> atomicReference = new AtomicReference();
8+
public void mylock(){
9+
Thread thread = Thread.currentThread();
10+
System.out.println(thread.getName()+"\t come in");
11+
while(!atomicReference.compareAndSet(null,thread)){
12+
13+
}
14+
}
15+
16+
public void myunlock(){
17+
Thread thread = Thread.currentThread();
18+
System.out.println(thread.getName()+"\t come out");
19+
atomicReference.compareAndSet(thread,null);
20+
}
21+
22+
public static void main(String[] args){
23+
SpinLockDemo spinLockDemo = new SpinLockDemo();
24+
new Thread(()->{
25+
spinLockDemo.mylock();
26+
try{
27+
TimeUnit.SECONDS.sleep(5);
28+
}catch (InterruptedException e){
29+
e.printStackTrace();
30+
}
31+
spinLockDemo.myunlock();
32+
},"AA").start();
33+
try{
34+
TimeUnit.SECONDS.sleep(1);
35+
}catch (InterruptedException e){
36+
e.printStackTrace();
37+
}
38+
new Thread(()->{
39+
spinLockDemo.mylock();
40+
try{
41+
TimeUnit.SECONDS.sleep(1);
42+
}catch (InterruptedException e){
43+
e.printStackTrace();
44+
}
45+
spinLockDemo.myunlock();
46+
},"BB").start();
47+
}
48+
}

0 commit comments

Comments
 (0)