1、ReadWriteLock读可以多个线程,写只能一个线程,写操作时不能被打断
```java
package com.ntuzy.juc_01.zf;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
*
*/
class MyCache {
private volatile Map<String, Object> map = new HashMap();
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public void write(String key, Object value) {
readWriteLock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName() + "开始写入" + key);
map.put(key, value);
System.out.println(Thread.currentThread().getName() + "结束写入");
} catch (Exception e) {
e.printStackTrace();
} finally {
readWriteLock.writeLock().unlock();
}
}
public void read(String key) {
readWriteLock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName() + "开始读取");
Object value = map.get(key);
System.out.println(Thread.currentThread().getName() + "结束读取" + value);
} catch (Exception e) {
e.printStackTrace();
} finally {
readWriteLock.readLock().unlock();
}
}
}
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyCache myCache = new MyCache();
//5个线程写
for (int i = 0; i < 5; i++) {
int finalI = i;
new Thread(() -> {
myCache.write(String.valueOf(finalI), finalI);
}, String.valueOf(i)).start();
}
//5个线程读
for (int i = 0; i < 5; i++) {
int finalI = i;
new Thread(() -> {
myCache.read(String.valueOf(finalI));
}, String.valueOf(i)).start();
}
}
}
2、BockingQueue阻塞队列
package com.ntuzy.juc_01.zf;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
BlockingQueue queue = new ArrayBlockingQueue(3);
// queue.add("a");
// queue.add("b");
// queue.add("c");
// System.out.println(queue.remove());
// System.out.println(queue.remove());
// System.out.println(queue.remove());
// System.out.println(queue.remove());
// System.out.println(queue.offer("a"));
// System.out.println(queue.offer("b"));
// System.out.println(queue.offer("c"));
// System.out.println(queue.offer("d"));
// System.out.println(queue.poll());
// System.out.println(queue.poll());
// System.out.println(queue.poll());
// System.out.println(queue.poll());
// queue.put("a");
// queue.put("b");
// queue.put("c");
// queue.put("d");
// System.out.println(queue.take());
// System.out.println(queue.take());
// System.out.println(queue.take());
// System.out.println(queue.take());
System.out.println(queue.offer("a"));
System.out.println(queue.offer("a"));
System.out.println(queue.offer("a"));
System.out.println(queue.offer("a",3, TimeUnit.SECONDS));
}
}
3、值传值和引用传递
package com.ntuzy.juc_01;
import sun.awt.image.IntegerInterleavedRaster;
/**
* @Author IamZY
* @create 2019/12/29 15:29
*/
public class TestTransferValue {
public void changeValue1(int age) {
age = 30;
}
public void changeValue2(Person person) {
person.setPersonName("xxx");
}
public void changeValue3(String str) {
str = "xxx";
}
public static void main(String[] args) {
TestTransferValue test = new TestTransferValue();
int age = 20;
test.changeValue1(age);
System.out.println("age---------" + age); // 20
Person p = new Person("abc");
test.changeValue2(p);
System.out.println("personName----------" + p.getPersonName()); // xxx
String str = "abc";
test.changeValue3(str);
System.out.println("string-------" + str); // abc 字符常量池
}
static class Person {
String personName;
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Person(String personName) {
this.personName = personName;
}
}
}
4、线程池ThreadPool
package com.ntuzy.juc_01.zf;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolDemo {
public static void main(String[] args) {
// ExecutorService executorService = Executors.newFixedThreadPool(5);//固定个数线程
// ExecutorService executorService = Executors.newSingleThreadExecutor();//单个线程
ExecutorService executorService = Executors.newCachedThreadPool();//缓存单个线程
try {
for (int i = 0; i < 10; i++) {
executorService.execute(() -> {
System.out.println(Thread.currentThread().getName() + "办理业务");
});
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
executorService.shutdown();
}
}
}
5、自定义线程池
注意:用官方自带的线程池创建线程,会出现OOM堆内存异常,一般自定义线程池
## 6、函数式接口
```java
//有一个输入,有一个返回值
Function<String, Integer> fun = (s) -> {
return 1;
};
System.out.println(fun.apply("zf"));
//有一个输入,有一个Boolean值
// Predicate<String> pre = new Predicate<String>() {
// @Override
// public boolean test(String s) {
// if(s.equals("zf")){
// return true;
// }else{
// return false;
// }
// }
// };
Predicate<String> pre = (s) -> {
if(s.equals("zf")){
return true;
}else{
return false;
}
};
System.out.println(pre.test("zf"));
//输入参数,没有返回值
Consumer<String> con = (s)->{
System.out.println(s);
};
con.accept("zf");
//没有输入参数,有返回参数
Supplier<String> sup = ()->{return "zf";};
System.out.println(sup.get());
7、流式计算
/**
* 偶数ID, 年纪大于24, 用户名转为大写,用户名字母倒排序,
* 值输出一个用户名字
*/
public class FunctionInterfaceDemo {
public static void main(String[] args) {
User u1 = new User(11, "a", 23);
User u2 = new User(12, "b", 24);
User u3 = new User(13, "c", 22);
User u4 = new User(14, "d", 28);
User u5 = new User(16, "e", 26);
List<User> users = Arrays.asList(u1, u2, u3, u4, u5);
Stream<User> streamUsers = users.stream();
streamUsers.filter((s)->{return s.getId()%2 ==0;})
.filter((s)->{return s.getAge() > 24;})
.map((s)->{return s.getName().toUpperCase();})
.sorted((s1,s2)->{return s2.compareTo(s1);})
.limit(1)
.forEach( System.out::println);
}
}
8、分支合并