Video tayyorlanmoqda
Thread qanday holatlardan o'tadi va ularni qayerda ko'rish mumkin?
Thread.State'da oltita holat bor: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED. Ularni getState() orqali yoki thread dump'da (jstack) ko'rasiz.
📖 Javob
- NEW — obyekt yaratildi,
start()hali chaqirilmadi. - RUNNABLE — ishlayapti yoki CPU navbatini kutyapti. Diqqat: socket'dan o'qib turgan thread ham ko'pincha RUNNABLE bo'lib ko'rinadi.
- BLOCKED —
synchronizedblokka kirmoqchi, lekin monitor boshqa thread'da. - WAITING — muddatsiz kutyapti:
wait(),join(),LockSupport.park(). - TIMED_WAITING — muddatli kutish:
sleep(1000),wait(500),poll(1, SECONDS). - TERMINATED —
run()tugadi yoki exception bilan chiqdi.
Thread t = new Thread(() -> sleepQuietly(5000));
System.out.println(t.getState()); // NEW
t.start();
Thread.sleep(100);
System.out.println(t.getState()); // TIMED_WAITING
Amalda bu holatlar thread dump o'qiganda kerak bo'ladi. Ko'p thread BLOCKED bo'lsa — qandaydir lock'da tiqilinch bor. Pool'dagi hamma thread WAITING bo'lsa — ular ish kutib o'tiribdi, bu normal.
Ro'yxatdan o'ting, shunda o'rgangan savollaringiz belgilanadi va progressingiz saqlanadi.