Java Thread Pool ExecutorService
1. Kolam benang
1.1 Dalam keadaan apa Anda menggunakan kumpulan benang?
1.2 Manfaat menggunakan kumpulan benang
2. ExcecutorService dan Executors
2.1 Pendahuluan
ExecutorService adalah antarmuka yang mewarisi pelaksana,
ExecutorService antarmuka publik Executor {}Pelaksana juga merupakan antarmuka, yang hanya berisi satu metode:
Eksekutor Antarmuka Publik {void execute (runnable command);} Antarmuka tingkat atas dari kumpulan utas di Java adalah excutor, tetapi benar-benar berbicara >> eksektor bukanlah kumpulan utas, tetapi hanya alat untuk mengeksekusi utas. Benang Nyata> Antarmuka Pool adalah ExecutorService.
3. Eksekutor
Ini adalah kelas pabrik statis, yang dapat menghasilkan berbagai jenis kumpulan benang, dan beberapa kode sumber adalah sebagai berikut:
public class Executors {//newFixedThreadPoolpublic static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());}//newCacheThreadPool public static ExecutorService newCachedThreadPool () {return new ThreadPoolExecutor (0, integer.max_value, 60l, timeunit.seconds, Synchronousqueue baru <Runnable> ()); } // newscheduledthreadpool public static staticExecutorService newscheduledThreadPool (int corePoolSize) {return new JadwalThreadPoolExecutor (corePoolSize); } // newstringooo}Pertama -tama mari kita lihat contoh spesifik dan gunakan contoh untuk menggambarkan persamaan dan perbedaan di antara mereka.
Paket utas; import java.util.concurrent.executorservice; impor java.util.concurrent.executors; import java.util.concurrent.schedululedExecutorservice;/*** dibuat oleh Yang pada 16-7-11. */kelas publik CH09_Executor {private static void run (ExecutorService ThreadPool) {for (int i = 1; i <5; i ++) {final int taskId = i; threadpool.execute (runnable baru () {@Override public void run () {for (int i = 1; i <5; i ++) {coba {thread.sleep (20);} catch (interruptException e) {e.printStackTrace ();} System.out.println ("++TaskId" {e.printStack ();} System.out.println ("++++TaskId () {E.PrintStack (); threadpool.shutdown (); Thread dari satu utas. jalankan (fixedthreadpool); // (2) // jalankan (singlethreadpool); // (3) // jalankan (jadwalThreadpool); // (4)}}4. 4 Kolam benang yang biasa digunakan
4.1 CachedThreadpool
CachedThreadPool akan membuat area cache yang menyimpan utas yang diinisialisasi, mengakhiri dan menghapus utas yang belum digunakan selama 6 detik dari cache.
Jika utas tersedia, gunakan utas yang dibuat sebelumnya. Jika utas tidak tersedia, buat utas baru.
.Reuse:
Pool tipe cache, pertama-tama periksa apakah ada utas yang telah dibuat di kolam. Jika ada, gunakan kembali. Jika tidak ada siapa pun, buat utas baru dan tambahkan ke kolam.
Gunakan skenario:
Kolam cache biasanya digunakan untuk melakukan beberapa tugas asinkron dengan masa hidup pendek, sehingga tidak banyak digunakan di beberapa server tipe daemon yang berorientasi koneksi.
Time Out:
Benang yang dapat digunakan kembali harus berupa utas di kolam dalam waktu kosong. Batas waktu default adalah 60 -an. Jika durasi idle ini terlampaui, instance utas akan diakhiri dan kumpulan akan dilepas.
Menyelesaikan:
Benang yang dimasukkan ke dalam cacheedthreadpool tidak perlu khawatir tentang akhirnya. Jika tidak aktif setelah timeout, itu akan diakhiri secara otomatis.
Contoh Penjelasan:
Hapus komentar (2) dan jalankan. Hasil operasi adalah sebagai berikut:
Pertama kali tugas pertama, ketiga kalinya tugas ketiga, ketiga kalinya dari kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kali kedua kalinya kedua kalinya kedua kali kedua kalinya kedua kalinya kedua kalinya kedua kalinya kedua kali kedua kali kedua kali kedua kalinya kedua kalinya kedua kali kedua kali kedua kalinya kedua kali kedua kali kedua kalinya kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kali kedua kalinya kedua kali kedua kali kedua kalinya kedua kali kedua kali kedua kalinya time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the fourth time of the fourth time of the fourth time of the first task, the fourth time of the fourth time of the first task, the fourth time of the fourth time of the first task, the fourth time of the fourth time of the first task, the fourth time of the fourth time of the first task, the fourth time of the fourth time of the fourth time of the first task, the fourth time of the fourth time of the fourth time of the first task, the fourth time of the fourth time of the fourth time of the first task, the fourth time of the fourth time of the fourth time of the first task, the Keempat kalinya keempat kalinya keempat kalinya tugas pertama, keempat kalinya keempat kalinya tugas pertama, keempat kalinya keempat kalinya keempat kalinya tugas pertama, keempat kalinya keempat kalinya keempat dari tugas pertama, tanggal keempat kalinya
Dari hasilnya, dapat dilihat bahwa 4 tugas dieksekusi secara bergantian.
4.2FixedThreadPool
Di FixedThreadpool, ada kumpulan ukuran tetap,
Jika tugas saat ini perlu dieksekusi melebihi ukuran kolam, maka banyak tugas keluar dalam keadaan menunggu sampai ada utas gratis untuk melaksanakan tugas.
Jika tugas yang saat ini harus dieksekusi lebih kecil dari ukuran kolam, benang idle tidak akan dihancurkan.
Reuse:
FixedThreadPool mirip dengan Cachethreadpool, dan juga dapat digunakan jika dapat digunakan kembali, tetapi Anda tidak dapat membuat utas baru kapan saja.
Nomor tetap
Keunikannya adalah bahwa pada setiap titik waktu, hanya ada jumlah utas aktif yang tetap. Pada saat ini, jika ada utas baru yang akan ditetapkan, itu hanya dapat ditempatkan di antrian lain dan menunggu sampai utas di utas saat ini berakhir dan dipindahkan keluar dari kolam secara langsung.
Time Out:
Tidak seperti cachethreadpool, fixedthreadpool tidak memiliki mekanisme idle
Gunakan skenario:
Oleh karena itu, sebagian besar FixedThreadPool ditujukan untuk beberapa utas bersamaan yang sangat stabil dan tetap, dan sebagian besar digunakan di server
Analisis Kode Sumber:
Dilihat dari kode sumber metode, kumpulan cache dan kumpulan tetap memanggil kumpulan dasar yang sama, tetapi parameternya berbeda.
Hitungan utas kumpulan tetap diperbaiki dan 0 detik idle (tanpa idle)
Jumlah utas kumpulan cache mendukung 0-integer.max_value (jelas, toleransi sumber daya host tidak dipertimbangkan sama sekali), 60 detik idle
Contoh Penjelasan:
Hapus komentar (1), dan hasil berjalan adalah sebagai berikut:
The first time of the first task, the third time of the third time of the first task, the third time of the second time of the second task, the third time of the second time of the second time of the second task, the third time of the third time of the second time of the second task, the third time of the third time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the second time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time of the fourth time dari keempat kalinya
Kumpulan utas ukuran tetap dibuat dengan kapasitas 3, dan kemudian 4 tugas dieksekusi dalam satu lingkaran. Dari hasil output, dapat dilihat bahwa 3 tugas pertama dieksekusi terlebih dahulu, dan kemudian utas idle melakukan tugas keempat.
4.3SingLetHreadExecutor
Hapus (3) Komentar. Lihat hasil eksekusi sebagai berikut:
Tugas pertama dari tugas pertama tugas pertama tugas kedua dari tugas pertama tugas ketiga dari tugas pertama tugas keempat tugas kedua tugas kedua tugas kedua tugas tugas kedua tugas tugas keempat tugas empat tugas ketiga tugas ketiga tugas ketiga tugas empat dari empat tugas empat tugas keempat, tugas empat dari empat tugas empat tugas keempat, tugas keempat, tugas keempat, tugas keempat, tugas keempat, tugas empat dari Four Tugas Foursh dari empat tugas Foursh dari empat tugas Foursh dari empat tugas Foursh dari Fourse Tugas Foursh dari Fourse TUGAS FORTH THE TUGAS FORTH FIRTH TUGAS KITH THE TUGAS KORIT KORI KITA Tugas keempat dari tugas keempat tugas keempat dari tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas keempat tugas tugas keempat dari tugas keempat dari tugas keempat
Keempat tugas dieksekusi secara berurutan.
4.4 TerjadwalThreadpool
JadwaltHreadPool adalah kumpulan utas ukuran tetap. Mirip dengan FixedThreadPool, tugas yang dieksekusi adalah tugas waktu.
Hasil yang diperoleh dengan menghapus anotasi (4) sama dengan hasil yang diperoleh dengan fixedthreadpool. Alasan utama untuk dijadwalkanThreadpool tidak ada di sini, tetapi tugas waktunya. Lihat contoh berikut:
Paket utas; import java.util.concurrent.executors; import java.util.concurrent.scheduledExecutorservice; import java.util.concurrent.timeunit;/*** dibuat oleh Yang pada 16-7-11. */kelas publik myscheduledTask mengimplementasikan runnable {private string tname; public myscheduledTask (nama string) {this.tname = name; } public void run () {System.out.println (tname+"Eksekusi penundaan tugas 2 detik!"); } public static void main (string [] args) {scheduledExecutorService scheduledpool = executors.newscheduledThreadpool (2); Terjadwal ExecutorService Singschedulepool = Executors.newsinglethreadscheduledExecutor (); MyscheduledTask mt1 = myscheduledTask baru ("mt1"); MyscheduledTask mt2 = myscheduledTask baru ("mt2"); // Mulai tugas MT1 dengan JadwalThreadPool untuk mengeksekusi jadwal yang dijadwalkan. // Mulai MT2 dengan SinglesCheduledThreadPool; Singschedulepool.schedule (MT2,2000, Timeunit.Milliseconds); jadwalpool.shutdown (); Singschedulepool.shutdown (); }}hasil:
Tugas MT1 menunda 2 detik untuk dieksekusi! Tugas MT2 menunda 2 detik untuk dieksekusi!
Hasilnya tidak akan ditampilkan setelah program dijalankan selama 2 detik, menunjukkan bahwa utas dieksekusi setelah 2 detik.
Terima kasih telah membaca, saya harap ini dapat membantu Anda. Terima kasih atas dukungan Anda untuk situs ini!