1、多線程怎么用 ?是什么?進(jìn)程和線程的區(qū)別?
進(jìn)程:操作系統(tǒng)上一塊獨立的運行的程序,有自己的數(shù)據(jù)管理和其他進(jìn)程數(shù)據(jù)是不共享的。
操作系統(tǒng)的一塊獨立區(qū)域,
線程:是一個程序中的并行線路之一,一個進(jìn)程中可能會有多個線程。
1、多線程使用:
(1)thread
private void thread () {
Thread thread = new Thread() {
@Override
public void run() {
System.out.println("Thread started!");
}
}
};
thread.start;
(2) runnable
可以重用runnable
private void runnable() {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Thread With Runnable Started!");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
(3)ThreadFactory
工廠方法的使用,統(tǒng)一的初始化線程操作,不用每一個線程都初始化一邊線程操作。
ThreadFactory factory = new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable);
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("ThreadFactory print");
}
};
Thread thread = factory.newThread(runnable);
thread.start();
(4) Executor
線程池 Executor
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("newCachedThreadPool print");
}
};
//可緩存線程池,如果緩存中沒有可用的,則移出60秒未使用過的線程
Executor executor = Executors.newCachedThreadPool();
//創(chuàng)建一個單線程的線程池
Executor executor = Executors.newSingleThreadExecutor();
//創(chuàng)建一個固定線程的線程池,可以用來處理集中瞬時爆發(fā)的任務(wù)
Executors.newFixedThreadPool(10);
//加延遲的線程池,支持定時和周期,一般情況下可替代timer
Executors.newScheduledThreadPool(10);
executor.execute(runnable);
(5) callable
相當(dāng)于帶返回值的runable
private void callable() {
Callable<String> callable = new Callable<String>() {
@Override
public String call() {
try {
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
};
ExecutorService service = Executors.newSingleThreadExecutor();
Future<String> future = service.submit(callable);
if (future.isDone()) {
try {
String result = future.get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
}
2、線程同步:
原子性 : AtomicInteger,AtomicBoolean
(1)volatile 關(guān)鍵字
當(dāng)寫一個volatile變量時,JMM會把該線程本地內(nèi)存中的變量強(qiáng)制刷新到主內(nèi)存中去
(2)synchronized
1.修飾方法,修飾代碼塊
//修飾方法
private synchronized void count() {
x += 1;
y += 1;
}
修飾代碼塊
//修飾修飾代碼塊
private void count() {
synchronized (this) {
x += 1;
y += 1;
}
}
另外,修飾代碼塊可以指定鎖
private Object object = new Object();
private void count() {
synchronized (object) {
x += 1;
y += 1;
}
}
- 修飾一個代碼塊,被修飾的代碼塊稱為同步語句塊,其作用的范圍是大括號{}括起來的代碼,作用的對象是調(diào)用這個代碼塊的對象;
- 修飾一個方法,被修飾的方法稱為同步方法,其作用的范圍是整個方法,作用的對象是調(diào)用這個方法的對象;
- 修改一個靜態(tài)的方法,其作用的范圍是整個靜態(tài)方法,作用的對象是這個類的所有對象;
- 修改一個類,其作用的范圍是synchronized后面括號括起來的部分,作用主的對象是這個類的所有對象。
3.使用synchronized的例子,線程安全的單例模式
public class Singleton {
//保證有序性,防止指令重排
private static volatile Singleton singleton;
public Singleton() {
}
private static Singleton getInstance() {
//先判斷對象是否已經(jīng)實例過,沒有實例化過才進(jìn)入加鎖代碼
if (singleton == null) {
//類對象加鎖
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}