Thread Pool

package com.example.threadpool;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity{

private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

textView=findViewById(R.id.textView);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// testCache();
// testFixed();
// testSingle();
testScheduled();
}
});
}

public void testCache(){
ExecutorService cacheThreadPool =
Executors.newCachedThreadPool();
for(int i=0;i<10;i++){
final int index = i;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
cacheThreadPool.execute(new Runnable() {
@Override
public void run() {
Log.d("TAG",Thread.currentThread().getName()+"----"+index);
}
});
}
}
public void testFixed(){
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

for(int i=0;i<10;i++){
final int index = i;
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
Log.d("TAG",Thread.currentThread().getName()+"---"+index);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
}
public void testSingle(){
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();

for(int i=0;i<10;i++){
final int index = i;
singleThreadPool.execute(new Runnable() {
@Override
public void run() {
Log.d("TAG",Thread.currentThread().getName()+"---"+index);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

}
});
}
}

public void testScheduled(){
Log.d("TAG","testScheduled");
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
Log.d("TAG","delay 3 seconds");
}
},3, TimeUnit.SECONDS);
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Log.d("TAG","delay 2 seconds and excute every 3 seconds");
}
},2,3,TimeUnit.SECONDS);
scheduledExecutorService.shutdown();
}
}

posted @ 2026-05-06 20:46  恢复记忆  阅读(16)  评论(0)    收藏  举报