首页 技术 正文
技术 2022年11月6日
0 收藏 713 点赞 802 浏览 2677 个字

线程安全:

当多个线程访问某一个类(对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法就是线程安全的)

synchronized:

可以在任意对象或方法上加锁,而加锁的这段代码称为“互斥区”或“临界区”

多个线程多个锁:

多个线程都有自己对应的锁

脏读:

在我们对一个对象的方法或对象加锁时,需要考虑业务的整体性,即为setValue/getValue方法同时加锁synchronized同步关键字,保证业务的原子性,不然会出现业务错误(也从侧面保证了数据的一致性)

线程间的通信:

使用notify/wait方法实现线程间的通信(注意这两个方法都是object类的方法)

wait/ notify 必须配合synchronized关键字使用

wait方法释放锁,notify方法不释放锁

CountDownLatch与锁无关

线程的三种实现方式Demo

 package com.jonychen.test; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; /**
* 线程的三种实现方式
*/
public class TestDemo { public static void main(String[] args){
/**
* 第一种方式:继承Thread类重写run()方法
*/
Thread thread=new ThreadDemo();
thread.start(); /**
* 第二种方式:实现Runnable接口重写run()方法
*/
RunnableDemo runnableDemo=new RunnableDemo();
Thread thread1=new Thread(runnableDemo);
thread1.start();
try {
thread1.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} /**
* 第三种方式:实现Callable接口重写call()方法
*/
CallableDemo callableDemo =new CallableDemo();
FutureTask<Integer> futureTask=new FutureTask<Integer>(callableDemo);
Thread thread2=new Thread(futureTask);
thread2.start();
try {
System.out.println("返回的参数为:"+futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} /**
* 第一种方式:继承Thread类重写run()方法
*/
class ThreadDemo extends Thread{
@Override
public void run() {
System.out.println("我是继承Thread类实现线程的一种方式!");
}
} /**
* 第二种方式:实现Runnable接口重写run()方法
*/
class RunnableDemo implements Runnable{
@Override
public void run() {
System.out.println("我是实现Runnable接口实现线程的一种方式");
}
} /**
* 第三种方式:实现Callable接口重写call()方法
*/
class CallableDemo implements Callable<Integer>{ @Override
public Integer call(){
return 666666;
}
}

输出截图:

并发编程系列小结(线程安全,synchronized,脏读,线程间的通信wait/notify,线程的三种实现方式Demo,可替代wait/notify的方法)

可替代wait/notify的方法

 package com.jonychen.test; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /**
*java.util.concurrent 类库中提供了 Condition 类来实现线程之间的协调,可以在 Condition 上调用 await() 方法使线程等待,
* 其它线程调用 signal() 或 signalAll() 方法唤醒等待的线程。
* 相比于 wait() 这种等待方式,await() 可以指定等待的条件,因此更加灵活。
*
* 使用 Lock 来获取一个 Condition 对象。
*/
public class ConcurrentDemo { private Lock lock=new ReentrantLock();
private Condition condition=lock.newCondition(); public void before(){
lock.lock();
try {
System.out.println("before");
condition.signalAll();
} finally {
lock.unlock();
}
} public void after(){
lock.lock();
try {
condition.await();
System.out.println("after");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
} } public static void main(String[] args){ ExecutorService executorService =Executors.newCachedThreadPool();
ConcurrentDemo concurrentDemo=new ConcurrentDemo();
executorService.execute(()->concurrentDemo.after());
executorService.execute(()->concurrentDemo.before());
} }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899