java并发编程实战
作者:互联网
文章目录
并发的基本概念
重入锁
一个钥匙和很多一样的锁。
原理:
重入锁保护方法A、方法B
方法A调用方法B可以成功。
重入锁有计数器,被获取一次钥匙就+1,变为0就释放锁。
耗时较长的地方不用锁,如IO,长时间计算的地方
package net.jcip.examples;
import java.math.BigInteger;
import javax.servlet.*;
import net.jcip.annotations.*;
/**
* CachedFactorizer
* <p/>
* Servlet that caches its last request and result
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class CachedFactorizer extends GenericServlet implements Servlet {
@GuardedBy("this") private BigInteger lastNumber;
@GuardedBy("this") private BigInteger[] lastFactors;
@GuardedBy("this") private long hits;
@GuardedBy("this") private long cacheHits;
public synchronized long getHits() {
return hits;
}
public synchronized double getCacheHitRatio() {
return (double) cacheHits / (double) hits;
}
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = null;
synchronized (this) {
++hits;
if (i.equals(lastNumber)) {
++cacheHits;
factors = lastFactors.clone();
}
}
if (factors == null) {
factors = factor(i);//耗时较长
synchronized (this) {
lastNumber = i;
lastFactors = factors.clone();
}
}
encodeIntoResponse(resp, factors);
}
void encodeIntoResponse(ServletResponse resp, BigInteger[] factors) {
}
BigInteger extractFromRequest(ServletRequest req) {
return new BigInteger("7");
}
BigInteger[] factor(BigInteger i) {
// Doesn't really factor
return new BigInteger[]{i};
}
}
条件变量
情景:当线程A获取锁,并进入方法后,发现因条件C不足,无法进行下去。
把当前线程用条件变量C管理:
- 进入条件集等待被唤醒
- 并释放锁
线程B发现,A可能满足条件C:
- 唤醒条件集等待的线程
- 释放锁
synchronized内部锁
每个对象都有一个隐式的锁,类也有隐式的锁。synchronized修饰的方法就利用了内部锁。
volatile
修饰的变量
- 不提供原子性。
- 对读取这个变量的其他线程都可见。
ThreadLocal线程局部变量
不在线程间共享变量,而是给每个线程提供一个实例。
创建线程
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
睡眠 Thread.sleep
public class SleepMessages {
public static void main(String args[])
throws InterruptedException {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0;
i < importantInfo.length;
i++) {
//Pause for 4 seconds
Thread.sleep(4000);
//Print a message
System.out.println(importantInfo[i]);
}
}
}
中断
标签:BigInteger,java,synchronized,factors,void,编程,并发,线程,public 来源: https://blog.csdn.net/qq_35459198/article/details/109776569