
本文共 4661 字,大约阅读时间需要 15 分钟。
Java���������������������������������������
Java���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������
���������������������������������������������������������������
������������������NGPV������������������������������������������������������������������������������������������������������������������5���Thread.NORM_PRIORITY���������������������������1���10���������������������
������������������������������
1. ������������������
Thread.getState()
��������������������������������� enumeration������������������������������������.
Thread thread = new Thread(runnable);System.out.println(thread.getState()); // ���������NEWthread.start();// ������������������������������while (!thread.isAlive()) { Thread.sleep(100);}System.out.println(thread.getState()); // ���������RUNNABLE
2. ������������������������
- ���������������
NEW
- ���������������
RUNNABLE
- ������������������������������������������������������������
- ���������������
TERMINATED
3. ������������������
������������stop()
���destroy()
���������������������������������������������������
private boolean isRunning = true;public void stop() { this.isRunning = false;}
4. ���������������������Join���
Thread.join()
������������������������������������������������������������������
public void run() { for (int i=0; i<100; i++) System.out.println(i); }public static void main(String[] args) { Thread thread = new Thread(new Runnable() { public void run() { for (int i=0; i<100; i++) System.out.println(i); } }); thread.start(); for (int i =0; i<100; i++) { System.out.println("main���������" + i); if(i %50 ==25) thread.join(); }}
���������������������������
���������������������setPriority(int)
���������������������5������������������1���10���������������������������CPU������������
public class TestPriority { public static void main(String[] args) { MyPriority thread = new MyPriority(); Thread t1 = new Thread(thread); t1.setPriority(1); t1.start(); Thread t2 = new Thread(thread); t2.setPriority(5); t2.start(); Thread t3 = new Thread(thread); t3.setPriority(10); t3.start(); while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { break; } System.out.println("������������������"); System.out.println(t1.getState()); System.out.println(t2.getState()); System.out.println(t3.getState()); } }}
���������������������Daemon threads���
������������������������������������������������������������������������������������������������������������������������������
public class TestDaemon { public static void main(String[] args) { // ������������������ Red red = new Red(); // ��������������������������������������������������� Thread thread = new Thread(red); thread.setDaemon(true); thread.start(); // ������������������������������ System.out.println("������������������������"); Life life = new Life(); new Thread(life).start(); }}
���������������������������������
������������������������
���������������JConsole���VisualVM���������������������������������������������������������������������
������������������
������������������������������������������������������������������������������������������
6. ������������������
���������������������������������������������������ExecutorService������������������������������������������������
������������������������
������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
