java实现思维导图_Java并发(思维导图)

f3e0c667a5a8517065338a8c5197036f.png

1,线程状态转换

8d60da0a867d5864f13f06b4dead3031.png

无限期等待:

15bf8c82fea651426c0dfe28e75bf596.png

限期等待:

4f2e4912161a05de458c31948b53a58f.png

线程生命流程:

0b1c9a4b610e29d1b3123be5cf9b84d7.png

2,实现方式

1eb0f72f57c9221e3d4339d3b8bbeb8c.png

代码实现样例【三种方式】:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo2;importjava.util.concurrent.Callable;public class test1_Runnable implementsRunnable{

@Overridepublic voidrun() {for(int i=0;i<50;i++){

System.out.println("当前线程:"+i);

}

}

}class test2_Callable implements Callable{private intnum;publictest2_Callable(){}public test2_Callable(intnum){this.num=num;

}

@Overridepublic String call() throwsException {for(int i=0;i<50;i++){

System.out.println(this.num+"线程:"+i);

}return num+"线程已完成";

}

}class test3_Thread extendsThread {private intnum;publictest3_Thread(){}public test3_Thread(intnum){this.num=num;

}

@Overridepublic voidrun() {for(int i=0;i<50;i++){

System.out.println(this.num+"线程:"+i);

}

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo2;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.FutureTask;public classClient {public static void main(String[] args) throwsExecutionException, InterruptedException {//实现 Runnable 接口//test1_Runnable instance=new test1_Runnable();//Thread thread=new Thread(instance);//Thread thread1=new Thread(instance);//thread.start();//thread1.start();//实现 Callable 接口//test2_Callable instance=new test2_Callable(1);//FutureTask ft=new FutureTask<>(instance);//Thread thread = new Thread(ft);//test2_Callable instance1=new test2_Callable(2);//FutureTask ft2=new FutureTask<>(instance1);//Thread thread1 = new Thread(ft2);//

//thread.start();//thread1.start();//System.out.println(ft.get());//System.out.println(ft2.get());//继承 Thread 类

test3_Thread thread1=new test3_Thread(1);

test3_Thread thread2=new test3_Thread(2);

thread1.start();

thread2.start();

}

}

View Code

3,基础线程机制

b435eab92fd1ac9bcc7b53ca378f5cf2.png

代码实现样例:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package com.cnblogs.mufasa.demo3;public classClient1 {public static voidmain(String[] args) throws InterruptedException {//设置守护线程 setDaemon 伴随线程而运行//Thread thread1 = new Thread(new test1_Runnable(1));//Thread thread2 = new Thread(new test3_Thread(2));//thread1.setDaemon(true);//thread1.start();//thread2.start();//sleep Thread.sleep(millisec) 方法会休眠当前正在执行的线程,millisec 单位为毫秒//sleep() 可能会抛出 InterruptedException,因为异常不能跨线程传播回 main() 中,因此必须在本地进行处理//Thread thread1 = new Thread(()->{//try {//System.out.println("延迟2s");//Thread.sleep(2000);//System.out.println("延迟结束");//} catch (InterruptedException e) {//e.printStackTrace();//}//});//thread1.start();//new Thread(new Runnable() {//@Override//public void run() {//System.out.println("延迟2s");//try {//Thread.sleep(2000);//System.out.println("延迟结束");//Thread.yield();//可以让出资源//Thread.sleep(2000);//} catch (InterruptedException e) {//e.printStackTrace();//}finally {//System.out.println("线程1运行结束");//}//}//}).start();//

//new Thread(()->{//try {//Thread.sleep(2000);//} catch (InterruptedException e) {//e.printStackTrace();//}finally {//System.out.println("线程2延迟结束");//}//}).start();

Thread t= new Thread(newRunnable(){public voidrun(){

System.out.println("First task started");

System.out.println("Sleeping for 2 seconds");try{

Thread.sleep(2000);

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.println("First task completed");

}

});

Thread t1= new Thread(newRunnable(){public voidrun(){

System.out.println("Second task completed");

}

});//在t执行完毕后t1执行

t.start(); //Line 15

t.join(); //Line 16

t1.start();

}

}

View Code

4,中断

195cc95e5d52fb1014c538e6e96edec4.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo4;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;import staticjava.lang.Thread.interrupted;class InterruptExample extendsThread {

@Overridepublic voidrun() {

System.out.println("sleep 2s");try{

Thread.sleep(2000);

System.out.println("Thread run");

}catch(InterruptedException e) {

System.out.println("休眠中断");

e.printStackTrace();

}

}

}class InterruptExample1 extendsThread {

@Overridepublic voidrun() {while (!interrupted()) {//..

}

System.out.println("Thread end");

}

}public classClient {public static voidmain(String[] args) {//InterruptExample thread1 = new InterruptExample();//thread1.start();//thread1.interrupt();//System.out.println("Main run");//InterruptExample1 thread2 = new InterruptExample1();//thread2.start();//thread2.interrupt();//ExecutorService executorService = Executors.newCachedThreadPool();//executorService.execute(() -> {//try {//Thread.sleep(2000);//System.out.println("Thread run");//} catch (InterruptedException e) {//System.out.println("线程程序池全体中断");//e.printStackTrace();//}//});//executorService.shutdownNow();//System.out.println("Main run");

ExecutorService executorService=Executors.newCachedThreadPool();

Future> future=executorService.submit(()->{

System.out.println("3");while (!interrupted()){

System.out.println("2");

}

System.out.println("1");

});

future.cancel(true);//强制中断

}

}

View Code

5,互斥同步

fb9eaa9bae63a0a62f3cbb8a0cf6dc56.png

代码实现:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo5;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;class SynchronizedExample {//同步代码块,作用于同一个对象

public voidfunc1() {synchronized (this) {for (int i = 0; i < 10; i++) {

System.out.print(i+ " ");

}

}

}

}class SynchronizedExample1 {//同步方法,作用于同一个对象

public synchronized voidfunc1() {for (int i = 0; i < 10; i++) {

System.out.print(i+ " ");

}

}

}class SynchronizedExample2 {//同步类,作用于整个类。

public voidfunc1() {synchronized(SynchronizedExample2.class){for (int i = 0; i < 10; i++) {

System.out.print(i+ " ");

}

}

}

}class SynchronizedExample3 {//同步静态方法,作用于整个类。

public static synchronized voidfunc1() {for (int i = 0; i < 10; i++) {

System.out.print(i+ " ");

}

}

}class LockExample {//ReentrantLock 是 java.util.concurrent(J.U.C)包中的锁。

private Lock lock=newReentrantLock();public voidfunc() {

lock.lock();try{for (int i = 0; i < 10; i++) {

System.out.print(i+ " ");

}

}finally{

lock.unlock();//确保解锁,防止死锁

}

}

}public classClient {public static voidmain(String[] args) {//synchronized-代码块 相同对象-同步代码块//SynchronizedExample e1=new SynchronizedExample();//ExecutorService executorService= Executors.newCachedThreadPool();//executorService.execute(()->e1.func1());//executorService.execute(()->e1.func1());//synchronized-代码块 不同对象-同步代码块,没有同步!!!//SynchronizedExample e1 = new SynchronizedExample();//SynchronizedExample e2 = new SynchronizedExample();//ExecutorService executorService = Executors.newCachedThreadPool();//executorService.execute(()->e1.func1());//executorService.execute(()->e2.func1());//synchronized-方法 不同对象,没有同步!!!//SynchronizedExample1 e1 = new SynchronizedExample1();//SynchronizedExample1 e2 = new SynchronizedExample1();//ExecutorService executorService = Executors.newCachedThreadPool();//executorService.execute(()->e1.func1());//executorService.execute(()->e2.func1());//synchronized-类 不同对象,有同步//SynchronizedExample2 e1 = new SynchronizedExample2();//SynchronizedExample2 e2 = new SynchronizedExample2();//ExecutorService executorService = Executors.newCachedThreadPool();//executorService.execute(()->e1.func1());//executorService.execute(()->e2.func1());//synchronized-静态方法 不同对象,有同步//SynchronizedExample3 e1 = new SynchronizedExample3();//SynchronizedExample3 e2 = new SynchronizedExample3();//ExecutorService executorService = Executors.newCachedThreadPool();//executorService.execute(()->e1.func1());//executorService.execute(()->e2.func1());//JDK实现的锁,作用一个对象

LockExample lockExample = newLockExample();

LockExample lockExample1= newLockExample();

ExecutorService executorService=Executors.newCachedThreadPool();

executorService.execute(()->lockExample.func());//executorService.execute(() -> lockExample1.func());

executorService.execute(() ->lockExample.func());

}

}

View Code

6,线程协作

d3cc193f53251043ea1949380e4d7aee.png

样例代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo6;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;classJoinExample{private class A extendsThread{

@Overridepublic voidrun(){for(int i=0;i<10;i++){

System.out.println("A:"+i);

}

}

}private class B extendsThread {privateA a;

B(A a) {this.a =a;

}

@Overridepublic voidrun() {try{

a.join();//先完成A线程,继续B线程

} catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println("B");

}

}public voidtest() {

A a= newA();

B b= newB(a);

b.start();

a.start();

}

}classWaitNotifyExample{public synchronized voidbefore(){

System.out.println("before");

notifyAll();//通知等待的线程

}public synchronized voidafter(){try{

wait();//先进行等待

} catch(InterruptedException e) {

e.printStackTrace();

}for(int i=0;i<10;i++){

System.out.print(i);

}

System.out.println("after");

}

}classAwaitSingalExample{private Lock lock=newReentrantLock();private Condition condition=lock.newCondition();public voidbefore(){

lock.lock();try{

System.out.println("before");

condition.signalAll();

}finally{

lock.unlock();

}

}public voidafter(){

lock.lock();try{

condition.await();

System.out.println("after");

}catch(InterruptedException e) {

e.printStackTrace();

}finally{

lock.unlock();

}

}

}public classClient {public static voidmain(String[] args) {//JoinExample example=new JoinExample();//example.test();//ExecutorService executorService = Executors.newCachedThreadPool();//WaitNotifyExample example = new WaitNotifyExample();//WaitNotifyExample example1 = new WaitNotifyExample();//executorService.execute(() -> example.after());//executorService.execute(() -> example1.after());//executorService.execute(() -> example.before());//executorService.execute(() -> example1.before());

ExecutorService executorService=Executors.newCachedThreadPool();

AwaitSingalExample example=newAwaitSingalExample();

executorService.execute(()->example.after());

executorService.execute(()->example.before());

}

}

View Code

7,J.U.C-AQS【java.util.concurrent】

87c8f51a2b2dde1e5387c4f16a46afe2.png

样例代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo7;import java.util.concurrent.*;classCyclicBarrier {private intparties;private intcount;privateRunnable barrierCommand;public CyclicBarrier(intparties,Runnable barrierAction){if (parties <= 0) throw newIllegalArgumentException();this.parties =parties;this.count =parties;this.barrierCommand =barrierAction;

}public CyclicBarrier(intparties) {this(parties, null);

}

}public classClient {public static void main(String[] args) throwsInterruptedException {final int clientCount=5;final int totalRequestCount=10;

Semaphore semaphore=newSemaphore(clientCount);

ExecutorService executorService=Executors.newCachedThreadPool();for(int i=0;i

executorService.execute(()->{try{

semaphore.acquire();

System.out.print(semaphore.availablePermits()+" ");

}catch(InterruptedException e) {

e.printStackTrace();

}finally{

semaphore.release();

}

});

}

executorService.shutdown();//Semaphore//final int totalThread =10;//CyclicBarrier cyclicBarrier=new CyclicBarrier(totalThread);//ExecutorService executorService=Executors.newCachedThreadPool();//for(int i=0;i{//System.out.println("before...");//try{//cyclicBarrier.wait();//源程序是await,这里只能使用wait//} catch (InterruptedException e) {//e.printStackTrace();//}//System.out.print("after..");//});//executorService.shutdown();//}//CountDownLatch实例//final int totalThread=10;//CountDownLatch countDownLatch=new CountDownLatch(3);//ExecutorService executorService= Executors.newCachedThreadPool();//for(int i=0;i<6;i++){//int finalI = i;//Thread.sleep(1000);//executorService.execute(()->{//System.out.print(finalI +"run-");//countDownLatch.countDown();//});//}//new Thread(()->{//try {//countDownLatch.await();//} catch (InterruptedException e) {//e.printStackTrace();//}//System.out.println("await等待线程");//}).start();//

//System.out.println("end");//executorService.shutdown();

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo7;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Semaphore;public classSemaphoreExample {public static voidmain(String[] args) {final int clientCount = 3;final int totalRequestCount = 10;

Semaphore semaphore= newSemaphore(clientCount);

ExecutorService executorService=Executors.newCachedThreadPool();for (int i = 0; i < totalRequestCount; i++) {

executorService.execute(()->{try{

semaphore.acquire();

System.out.print(semaphore.availablePermits()+ " ");

}catch(InterruptedException e) {

e.printStackTrace();

}finally{

semaphore.release();

}

});

}

executorService.shutdown();

}

}

View Code

8.J.U.C-其他组件

ce56f1981e12e8aa36059aff881f1c2e.png

样例代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo8;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ForkJoinPool;importjava.util.concurrent.Future;importjava.util.concurrent.RecursiveTask;public class ForkJoinExample extends RecursiveTask{private final int threshold=5;private intfirst;private intlast;public ForkJoinExample(int first,intlast){this.first=first;this.last=last;

}

@OverrideprotectedInteger compute() {int result=0;if(last-first<=threshold){//运算量足够小直接计算

for(int i=first;i<=last;i++){

result+=i;

}

}else{//拆分成小任务

int middle=first+(last-first)/2;

ForkJoinExample leftTask=newForkJoinExample(first,middle);

ForkJoinExample rightTask=new ForkJoinExample(middle+1,last);

leftTask.fork();

rightTask.fork();

result=leftTask.join()+rightTask.join();

}returnresult;

}public static int sum(int first,intlast){int sum=0;for(int i=first;i<=last;i++){

sum+=i;

}returnsum;

}public static void main(String[] args) throwsExecutionException, InterruptedException {//普通方法计算//System.out.println(sum(1, 1000000000));//并行计算方法//ForkJoinExample example = new ForkJoinExample(1, 1000000000);//ForkJoinPool forkJoinPool = new ForkJoinPool();//Future result=forkJoinPool.submit(example);//System.out.println(result.get());

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo8;importjava.util.concurrent.Callable;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.Future;importjava.util.concurrent.FutureTask;public classFutureTaskExample{public static void main(String[] args) throwsExecutionException, InterruptedException {

FutureTask futureTask=new FutureTask<>(new Callable() {

@Overridepublic Integer call() throwsException {int result=0;for(int i=0;i<10;i++){

Thread.sleep(500);

System.out.println("thread-A:"+i);

result+=i;

}returnresult;

}

});

Thread computeThread=newThread(futureTask);

computeThread.start();

Thread otherThread=new Thread(()->{

System.out.println("other task is running...");try{

Thread.sleep(1000);

}catch(InterruptedException e) {

e.printStackTrace();

}

});

otherThread.start();

System.out.println("运行正常");

System.out.println(futureTask.get());

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo8;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.BlockingQueue;public classProducerConsumer {private static BlockingQueue queue=new ArrayBlockingQueue<>(5);private static class Producer extendsThread{

@Overridepublic voidrun() {try{

queue.put("product");

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(queue.size()+"produce..");

}

}private static class Consumer extendsThread{

@Overridepublic voidrun() {try{

String product=queue.take();

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(queue.size()+"consume..");

}

}public static void main(String[] args) {//类似缓存,协调速率

for(int i=0;i<2;i++){

Producer producer=newProducer();

producer.start();

}for(int i=0;i<5;i++){

Consumer consumer=newConsumer();

consumer.start();

}for (int i = 0; i < 3; i++) {

Producer producer= newProducer();

producer.start();

}

}

}

View Code

9.线程不安全

b1abe64e767c78475f12c18b83bb6c79.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo9;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;classThreadUnsafeExample{private int cnt=0;//public synchronized void add(){//输出为1000,同步后输出正常

public void add(){//不同步后,输出不正常

cnt++;

}public intget(){returncnt;

}

}public classClient {public static void main(String[] args) throwsInterruptedException {final int threadSize=1000;

ThreadUnsafeExample example=newThreadUnsafeExample();final CountDownLatch countDownLatch=newCountDownLatch(threadSize);

ExecutorService executorService=Executors.newCachedThreadPool();for(int i=0;i

executorService.execute(()->{

example.add();

countDownLatch.countDown();

});

}

countDownLatch.await();

executorService.shutdown();

System.out.println(example.get());

}

}

View Code

10.Java内存模式

c18949ef8875e39b79c9011292aac361.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo10;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.atomic.AtomicInteger;public classAtomicExample {private AtomicInteger cnt=newAtomicInteger();public voidadd(){

cnt.incrementAndGet();//原子自增

}public intget(){returncnt.get();

}public static void main(String[] args) throwsInterruptedException {final int threadSize=1000;

AtomicExample example=newAtomicExample();final CountDownLatch countDownLatch=newCountDownLatch(threadSize);

ExecutorService excutorService=Executors.newCachedThreadPool();for(int i=0;i

excutorService.execute(()->{

example.add();

countDownLatch.countDown();

});

}

countDownLatch.await();

excutorService.shutdown();

System.out.println(example.get());

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo10;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;public classAtomicSynchronizedExample {private int cnt=0;public synchronized voidadd(){

cnt++;

}public synchronized intget(){returncnt;

}public static void main(String[] args) throwsInterruptedException {final int threadSize = 1000;

AtomicSynchronizedExample example=newAtomicSynchronizedExample();final CountDownLatch countDownLatch = newCountDownLatch(threadSize);

ExecutorService executorService=Executors.newCachedThreadPool();for(int i=0;i

executorService.execute(()->{

example.add();

countDownLatch.countDown();

});

}

countDownLatch.await();

executorService.shutdown();

System.out.println(example.get());

}

}

View Code

11.线程安全

21e5462bca399512ad3f1a68a4a23521.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo11;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.atomic.AtomicInteger;public classAtomicIntegerExample {private AtomicInteger cnt=newAtomicInteger();public voidadd(){

cnt.incrementAndGet();

}public intget(){returncnt.get();

}public static void main(String[] args) throwsInterruptedException {

AtomicIntegerExample example=newAtomicIntegerExample();

ExecutorService executorService=Executors.newCachedThreadPool();for(int i=0;i<10;i++){

executorService.execute(()->example.add());

}

Thread.sleep(100);

example.add();

System.out.println(example.get());

executorService.shutdown();

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo11;importjava.util.Collections;importjava.util.HashMap;importjava.util.Map;public classImmutableExample {public static voidmain(String[] args) {

Map map=new HashMap<>();

map.put("b",2);

System.out.println(map.get("b"));

Map unmodifiableMap= Collections.unmodifiableMap(map);//相当于设置一下

System.out.println(unmodifiableMap.get("b"));

unmodifiableMap.put("a",1);

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo11;importjava.util.concurrent.Executor;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;public classStack_closedExample {public voidadd100() {int cnt=0;for(int i=0;i<100;i++){

cnt++;

}

System.out.println(cnt);

}public static voidmain(String[] args) {

Stack_closedExample example=newStack_closedExample();

ExecutorService executorService=Executors.newCachedThreadPool();

executorService.execute(()->example.add100());

executorService.execute(()->example.add100());

executorService.shutdown();

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo11;public classThreadLocalExample {public static voidmain(String[] args) {

ThreadLocal threadLocal=newThreadLocal();

Thread thread1=new Thread(()->{

threadLocal.set(1);try{

Thread.sleep(1000);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(threadLocal.get());

threadLocal.remove();

});

Thread thread2=new Thread(()->{

threadLocal.set(2);

threadLocal.remove();

});

thread1.start();

thread2.start();

}

}

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo11;public classThreadLocalExample1 {public static voidmain(String[] args) {

ThreadLocal threadLocal1=newThreadLocal();

ThreadLocal threadLocal2=newThreadLocal();

Thread thread1= new Thread(() ->{

threadLocal1.set(1);

threadLocal2.set(1);

});

Thread thread2= new Thread(() ->{

threadLocal1.set(2);

threadLocal2.set(2);

});

thread1.start();

thread2.start();

}

}

View Code

12.锁优化

3336ea9490802b74e5ef79ad72a12e87.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.cnblogs.mufasa.demo12;public classStringExample {public staticString concatString(String s1, String s2, String s3) {return s1 + s2 +s3;

}public staticString concatString1(String s1, String s2, String s3) {

StringBuilder sb= newStringBuilder ();

sb.append(s1);

sb.append(s2);

sb.append(s3);returnsb.toString();

}public static voidmain(String[] args) {

System.out.println(concatString("12","34","56"));

System.out.println(concatString1("12","34","56"));

}

}

View Code

13.多线程开发良好的实践

2a04489e353bded9042b9d65d8650b52.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/303477.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

这本免费小书,带你征服 GitHub!

GitHub 漫游指南&#xff0c;一本有趣又通俗的 GitHub 教程&#xff0c;想要成为 star 之王么&#xff1f;今天给大家分享一份通俗易懂的 GitHub 学习教程&#xff0c;即《GitHub 漫游指南》。看过了不下十套 GitHub 教程了&#xff0c;小编愿称它为最强&#xff01;虽然没有 G…

jQuery formValidator表单验证插件4.1.0 下载 演示 文档 可换肤 代码生成器

更新记录: 2011/6/5 jQuery formValidator 4.1.0 ver申明&#xff1a;1、所有DEMO引用jQuery类库的时候&#xff0c;类库后面直接跟了版本号——表示插件支持的最高类库版本号2、插件的命名&#xff1a;插件名版本号&#xff0c;压缩版&#xff1a;插件名版本号min新增以下功能…

java context.write_Channel.write() 和 ChannelHandlerContext.write() 的区别

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。看了下netty 源代码, 终于明白了Channel.write() 和 ChannelHandlerContext.write() 的区别了网上说的都不是很清楚首先注明我的netty版本io.nettynetty-all4.0.36.FinalChannelPipeline处理ChannelH…

美国劳工部揭露中国女人大数据,看完彻底傻眼了……

导读&#xff1a;“中国人的勤奋&#xff0c;令世界惊叹和汗颜&#xff0c;甚至有一点恐惧。”——科斯&#xff0c;诺贝尔经济学奖得主那么中国人的“勤奋”能否用数据来量化一下呢&#xff1f;答案是可以的。另外&#xff0c;如果以性别来区分&#xff0c;究竟是中国男性更勤…

计算机女神,互联网第一夫人!

大家好&#xff0c;我是鱼皮&#xff0c;今天带大家认识一位计算机领域的女神&#xff0c;有关她的故事非常有趣&#xff01;先聊聊我是如何认识女神的吧。那是我在大四做毕业设计的时候&#xff0c;误打误撞地选了一个图像处理相关的课题&#xff0c;要做一个显微图像处理分析…

NET问答: 如何从 string 中挖出所有的 number ?

咨询区 van:我现在有一个需求&#xff0c;想从 string 中找到所有的 number 并提取出来。举例如下&#xff1a;string test "1 hello" string test1 " 1 world" string test2 "helloworld 99"请问我该如何做&#xff1f;回答区 Tabares:这个简…

大数据告诉你:学历真的能改变命运

央视新闻曾做过关于高考的调查&#xff0c;结果有七成网友支持高考取消数学&#xff0c;看到新闻后&#xff0c;有一位网友却一针见血地评论道&#xff1a;数学考试存在的意义就是把这七成网友筛选掉。的确&#xff0c;虽然买菜不需要专业数学知识&#xff0c;但数学可以决定我…

mysql控制台教程视频教程_mysql 控制台操作

一、连接mysql数据库1.首先打开cmd进入dos窗口2.切换至目录&#xff1a;D:\MySql5.0\mysql-5.0.51b-win32\bin(即&#xff1a;mysql安装目录下面的bin目录&#xff0c;该目录内有很多exe执行文件)3.键入命令&#xff1a;mysql -uroot -p 回车&#xff0c;提示输入密码&#xff…

Dapr微服务应用开发系列5:发布订阅构建块

题记&#xff1a;这篇介绍发布订阅构建块&#xff0c;这是对事件驱动架构设计的一种实现落地。注&#xff1a;对于“Building Blocks”这个词组的翻译&#xff0c;我之前使用了“构件块”&#xff0c;现在和官方文档&#xff08;Dapr中文社区的贡献&#xff09;保持一致&#x…

【资源】机器学习资料包来袭

近几年&#xff0c;机器学习一直很火&#xff0c;小编也有意识地收集了机器学习相关的资源&#xff0c;经过长时间的积累和沉淀&#xff0c;已经拥有将近17G的吴恩达老师、李宏毅老师和王保明老师机器学习资料和视频。现在&#xff0c;小编准备将这些资料免费分享给大家&#x…

java 字符串 去除_java 字符串中去除特定的字符

java String字符串 去除特定的字符 程序如下package com.xing.test;import java.util.regex.Matcher;import java.util.regex.Pattern;/**** author Yinxing**/public class NotString {public static void main(String[] args) {// TODO Auto-generated method stubString st…

深度学习了40万个表情,一大波AI 表情包来了

自从有了表情包&#xff0c;跟人聊天时的第一反应&#xff0c;就是去找找看有什么适合的表情。有一类表情包&#xff0c;形式是文字图&#xff0c;尤其能够精妙地抒发和传递感情。在这一点上&#xff0c;可能全世界的网友都一样。好用的表情永远不嫌多&#xff0c;而且似乎总是…

网关Ocelot功能演示安排的明明白白~~~

前言网关(Gateway)在微服务架构中至关重要&#xff0c;可以将其理解为是外部客户端(前端、MVC后台等调用方)与后台服务的连接点&#xff0c;通过这层可以做统一的处理&#xff0c;比如路由、身份认证和授权、服务治理等&#xff1b;网关的好处&#xff1a;统一入口&#xff0c;…

通过Dapr实现一个简单的基于.net的微服务电商系统

本来想在Dpar 1.0GA时发布这篇文章&#xff0c;由于其他事情耽搁了放到现在。时下微服务和云原生技术如何如荼&#xff0c;微软也不甘示弱的和阿里一起适时推出了Dapr&#xff08;https://dapr.io/&#xff09;&#xff0c;园子里关于dapr的文章不太多&#xff0c;所以今天就借…

基于 Python 自建分布式高并发 RPC 服务

RPC&#xff08;Remote Procedure Call&#xff09;服务&#xff0c;也即远程过程调用&#xff0c;在互联网企业技术架构中占据了举足轻重的地位&#xff0c;尤其在当下微服务化逐步成为大中型分布式系统架构的主流背景下&#xff0c;RPC 更扮演了重要角色。Google 开源了 gRPC…

程序员江湖鄙视链大全,看看你处于链条的哪一级?

有人的地方就有江湖。程序员&#xff0c;是一个知识、智商、都异于常人的 群体&#xff0c;有人总结了程序员江湖等级鄙视链的方法和流程。老婆漂亮的程序员鄙视老婆不漂亮的程序员鄙视有女友的程序员鄙视单身狗程序员而在单身狗之间&#xff0c;才有了语言&#xff0c;编辑器和…

java什么是静态_什么是java静态

什么是java静态java静态包括静态变量、静态方法、静态初始化块&#xff0c;以下是静态的详解。(推荐教程&#xff1a;java教程)1. 什么是静态变量大家都知道&#xff0c;我们可以基于一个类创建多个该类的对象&#xff0c;每个对象都拥有自己的成员&#xff0c;互相独立。然而在…

如何在 .NET 程序万种死法中有效的生成 Dump (上)

一&#xff1a;背景相信很多人都知道通过 任务管理器 抓取dump&#xff0c;虽然简单粗暴&#xff0c;但无法满足程序的无数种死法&#xff0c;比如&#xff1a;内存膨胀&#xff0c;程序爆炸CPU爆高&#xff0c;程序累死应用无响应&#xff0c;用户气死意外退出&#xff0c;和人…

74款app源码,值得你拥有的干货

最近&#xff0c;小编一直在整理一些app的源码&#xff0c;如&#xff1a;BiliClient&#xff08;仿bilibili客户端&#xff09;、WeChat高仿微信、知乎专栏App、Compass&#xff08;MIUI指南针的社区开源版&#xff09;等。现在小编打算将这些资料免费分享给大家&#xff01;&…

监控系统简介:使用 Prometheus 与 Grafana

注&#xff1a;本文虽以 Docker 进行演示&#xff0c;但 Docker 并不是必须的&#xff0c;相关软件也可以直接安装到计算机上背景如果我们是Web应用的开发者&#xff0c;会对响应时间、接口的稳定性等比较敏感&#xff0c;在站点尚未部署到生产环境时&#xff0c;我们有充足的时…