Skip to content

Commit 51e85e1

Browse files
测试线程池
1 parent 4cf822f commit 51e85e1

File tree

7 files changed

+275
-9
lines changed

7 files changed

+275
-9
lines changed

app/src/main/java/com/isoftstone/huidingc/testqmui/fragment/NewsFragment.java

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected void initListener() {
6565
@Override
6666
protected void initData() {
6767
setDefaultWebSettings(wb);
68+
wb.loadUrl("https://www.baidu.com/?tn=39042058_30_oem_dg");
6869
}
6970

7071
private void setDefaultWebSettings(WebView webView) {

app/src/main/java/com/isoftstone/huidingc/testqmui/fragment/PartyFragment.java

+73-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.os.Bundle;
6+
import android.support.annotation.NonNull;
67
import android.view.View;
8+
import android.widget.Button;
79
import android.widget.TextView;
810

911
import com.isoftstone.huidingc.testqmui.R;
1012
import com.isoftstone.huidingc.testqmui.base.BaseFragment;
13+
import com.isoftstone.huidingc.testqmui.thread.Priority;
14+
import com.isoftstone.huidingc.testqmui.thread.PriorityExecutor;
15+
import com.isoftstone.huidingc.testqmui.thread.PriorityRunnable;
16+
import com.isoftstone.huidingc.testqmui.utils.LogUtils;
17+
18+
import java.util.concurrent.ExecutorService;
19+
import java.util.concurrent.Executors;
20+
import java.util.concurrent.ScheduledExecutorService;
21+
import java.util.concurrent.ThreadFactory;
22+
import java.util.concurrent.atomic.AtomicInteger;
1123

1224
import butterknife.BindView;
1325
import butterknife.ButterKnife;
@@ -20,8 +32,9 @@
2032
* @version
2133
*/
2234
public class PartyFragment extends BaseFragment {
23-
@BindView(R.id.tv_name)
24-
TextView tvName;
35+
private static final String TAG = "PartyFragment";
36+
@BindView(R.id.bt1)
37+
Button bt1;
2538
Unbinder unbinder;
2639
private Activity activity;
2740

@@ -41,7 +54,7 @@ public static PartyFragment getInstance(String data){
4154

4255
@Override
4356
protected int getLayoutId() {
44-
return R.layout.fragment_layout;
57+
return R.layout.party_fragment_layout;
4558
}
4659

4760
@Override
@@ -51,15 +64,14 @@ protected void initViews(View view) {
5164

5265
@Override
5366
protected void initListener() {
54-
67+
bt1.setOnClickListener(onClickListener);
5568
}
5669

5770
@Override
5871
protected void initData() {
5972
Bundle bundle = getArguments();
6073
if(null != bundle){
6174
String name = bundle.getString("name");
62-
tvName.setText(name);
6375
}
6476
}
6577

@@ -68,4 +80,60 @@ public void onDestroyView() {
6880
super.onDestroyView();
6981
unbinder.unbind();
7082
}
83+
84+
View.OnClickListener onClickListener = new View.OnClickListener() {
85+
@Override
86+
public void onClick(View v) {
87+
switch (v.getId()){
88+
case R.id.bt1:
89+
function1();
90+
break;
91+
default:
92+
break;
93+
}
94+
}
95+
};
96+
97+
/**
98+
* ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue)
99+
* ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long keepAliveTime, TimeUnit unit,BlockingQueue workQueue,RejectedExecutionHandler handler)
100+
* ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler)
101+
* ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory, RejectedExecutionHandler handler)
102+
*
103+
* corePoolSize: 线程池维护线程的最少数量
104+
* maximumPoolSize:线程池维护线程的最大数量
105+
* keepAliveTime: 线程池维护线程所允许的空闲时间
106+
* unit: 线程池维护线程所允许的空闲时间的单位
107+
* workQueue: 线程池所使用的缓冲队列
108+
* threadFactory:线程池用于创建线程
109+
* handler: 线程池对拒绝任务的处理策略
110+
*/
111+
private void function1() {
112+
ExecutorService executorService = new PriorityExecutor(5,false);
113+
for (int i = 0; i < 20; i++) {
114+
PriorityRunnable priorityRunnable = new PriorityRunnable(Priority.NORMAL, new Runnable() {
115+
@Override
116+
public void run() {
117+
LogUtils.e(TAG,Thread.currentThread().getName()+"优先级正常");
118+
}
119+
});
120+
121+
if (i % 3 == 1) {
122+
priorityRunnable = new PriorityRunnable(Priority.HIGH, new Runnable() {
123+
@Override
124+
public void run() {
125+
LogUtils.e(TAG, Thread.currentThread().getName()+"优先级高");
126+
}
127+
});
128+
} else if (i % 5 == 0) {
129+
priorityRunnable = new PriorityRunnable(Priority.LOW, new Runnable() {
130+
@Override
131+
public void run() {
132+
LogUtils.e(TAG, Thread.currentThread().getName()+"优先级低");
133+
}
134+
});
135+
}
136+
executorService.execute(priorityRunnable);
137+
}
138+
}
71139
}

app/src/main/java/com/isoftstone/huidingc/testqmui/network/utils/GsonUtil.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.isoftstone.huidingc.testqmui.network.utils;
22

3+
import android.text.TextUtils;
4+
35
import com.google.gson.Gson;
46
import com.google.gson.JsonObject;
57
import com.google.gson.reflect.TypeToken;
@@ -44,11 +46,13 @@ public static BaseData toObject(String data) throws JSONException {
4446
* @return
4547
*/
4648
public static <T> ArrayList<T> jsonToArrayList(String json,Class<T> clazz){
47-
Type type = new TypeToken<ArrayList<JsonObject>>(){}.getType();
48-
ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json,type);
4949
ArrayList<T> list = new ArrayList<>();
50-
for (JsonObject jsonObject : jsonObjects) {
51-
list.add(new Gson().fromJson(jsonObject,clazz));
50+
if(!json.equals("null") && !json.equals("") && !TextUtils.isEmpty(json)){
51+
Type type = new TypeToken<ArrayList<JsonObject>>(){}.getType();
52+
ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json,type);
53+
for (JsonObject jsonObject : jsonObjects) {
54+
list.add(new Gson().fromJson(jsonObject,clazz));
55+
}
5256
}
5357
return list;
5458
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.isoftstone.huidingc.testqmui.thread;
2+
3+
/**
4+
* @auther huidingc
5+
* @date 2018/3/16 14:53
6+
* @description Priority
7+
*
8+
* 线程优先级
9+
*/
10+
public enum Priority {
11+
HIGH,NORMAL,LOW
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.isoftstone.huidingc.testqmui.thread;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import java.util.Comparator;
6+
import java.util.concurrent.BlockingQueue;
7+
import java.util.concurrent.PriorityBlockingQueue;
8+
import java.util.concurrent.RejectedExecutionHandler;
9+
import java.util.concurrent.ThreadFactory;
10+
import java.util.concurrent.ThreadPoolExecutor;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
import java.util.concurrent.atomic.AtomicLong;
14+
15+
/**
16+
* @auther huidingc
17+
* @date 2018/3/16 15:00
18+
* @description PriorityExecutor
19+
* 创建线程池
20+
*/
21+
public class PriorityExecutor extends ThreadPoolExecutor {
22+
/**
23+
* 线程池维护线程的最少数量
24+
* 核心线程池大小
25+
*/
26+
private static final int CORE_POOL_SIZE = 5;
27+
/**
28+
* 最大线程池队列大小
29+
*/
30+
private static final int MAXIMUM_POOL_SIZE = 256;
31+
/**
32+
* 保持存活时间,当线程数大于corePoolSize的空闲线程能保持的最大时间。
33+
*/
34+
private static final int KEEP_ALIVE_TIME = 1;
35+
/**
36+
* 主要获取添加任务
37+
*/
38+
private static final AtomicLong SEQ_SEED = new AtomicLong(0);
39+
40+
41+
public PriorityExecutor(boolean fifo){
42+
this(CORE_POOL_SIZE,fifo);
43+
}
44+
45+
/**
46+
* 常见几种BlockingQueue实现
47+
* 1. ArrayBlockingQueue : 有界的数组队列
48+
* 2. LinkedBlockingQueue : 可支持有界/无界的队列,使用链表实现
49+
* 3. PriorityBlockingQueue : 优先队列,可以针对任务排序
50+
* 4. SynchronousQueue : 队列长度为1的队列,和Array有点区别就是:client thread提交到block queue会是
51+
* 一个阻塞过程,直到有一个worker thread连接上来poll task。
52+
* @param poolSize
53+
* @param fifo
54+
*/
55+
public PriorityExecutor(int poolSize, boolean fifo) {
56+
this(poolSize, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(MAXIMUM_POOL_SIZE, fifo ? FIFO : LIFO), threadFactory);
57+
}
58+
59+
public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
60+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
61+
}
62+
63+
public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
64+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
65+
}
66+
67+
public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
68+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
69+
}
70+
71+
public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
72+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
73+
}
74+
75+
/**
76+
* 创建线程工厂
77+
*/
78+
private static ThreadFactory threadFactory = new ThreadFactory() {
79+
private AtomicInteger mCount = new AtomicInteger(1);
80+
81+
@Override
82+
public Thread newThread(@NonNull Runnable r) {
83+
return new Thread(r,"download#"+mCount.getAndIncrement());
84+
}
85+
};
86+
87+
/**
88+
* 线程队列方式,先进先出
89+
*/
90+
private static Comparator<Runnable> FIFO = new Comparator<Runnable>() {
91+
@Override
92+
public int compare(Runnable runnable1, Runnable runnable2) {
93+
if(runnable1 instanceof PriorityRunnable && runnable2 instanceof PriorityRunnable){
94+
PriorityRunnable r1 = (PriorityRunnable) runnable1;
95+
PriorityRunnable r2 = (PriorityRunnable) runnable2;
96+
int result = r1.priority.ordinal() - r2.priority.ordinal();
97+
return result == 0 ? (int) (r1.SEQ - r2.SEQ) : result;
98+
}else{
99+
return 0;
100+
}
101+
}
102+
};
103+
104+
/**
105+
* 线程队列方式,后进先出
106+
*/
107+
private static Comparator<Runnable> LIFO = new Comparator<Runnable>() {
108+
@Override
109+
public int compare(Runnable runnable1, Runnable runnable2) {
110+
if(runnable1 instanceof PriorityRunnable && runnable2 instanceof PriorityRunnable){
111+
PriorityRunnable r1 = (PriorityRunnable) runnable1;
112+
PriorityRunnable r2 = (PriorityRunnable) runnable2;
113+
int result = r1.priority.ordinal() - r2.priority.ordinal();
114+
return result == 0 ? (int) (r2.SEQ - r1.SEQ) : result;
115+
}else{
116+
return 0;
117+
}
118+
}
119+
};
120+
121+
/**
122+
* 判断当前线程是否繁忙
123+
* @return
124+
*/
125+
public boolean isBusy(){
126+
return getActiveCount() >= getCorePoolSize();
127+
}
128+
129+
/**
130+
* 提交任务
131+
* @param runnable
132+
*/
133+
@Override
134+
public void execute(Runnable runnable) {
135+
if(runnable instanceof PriorityRunnable){
136+
((PriorityRunnable) runnable).SEQ = SEQ_SEED.getAndIncrement();
137+
}
138+
super.execute(runnable);
139+
}
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.isoftstone.huidingc.testqmui.thread;
2+
3+
/**
4+
* @auther huidingc
5+
* @date 2018/3/16 14:56
6+
* @description PriorityRunnable
7+
* 带有优先级的Runnable对象
8+
*/
9+
10+
public class PriorityRunnable implements Runnable {
11+
public Priority priority;
12+
private Runnable runnable;
13+
/**
14+
* 任务唯一标示
15+
*/
16+
long SEQ;
17+
18+
public PriorityRunnable(Priority priority,Runnable runnable){
19+
this.priority = priority == null ? Priority.NORMAL : priority;
20+
this.runnable = runnable;
21+
}
22+
23+
@Override
24+
public void run() {
25+
this.runnable.run();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
>
8+
<Button
9+
android:id="@+id/bt1"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:text="固定数量线程池"
13+
/>
14+
</LinearLayout>

0 commit comments

Comments
 (0)