
本文共 4932 字,大约阅读时间需要 16 分钟。
Java���������������������
���Java���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Java���������������������������������������������������������������������������������������������������������������������������
������������
���Java������������������������Runnable
���������Thread
���������������Runnable
������������������������������������������������������������������������������������������������������
������Runnable
���������
Runnable task = new Runnable() { @Override public void run() { System.out.println("Runnable������������������"); }};
������Thread
������������������
Thread t = new Thread(task);t.start();
������������������������������������������������run()
������������������������������������������������������
���������������������
������������������������������������������������������������������������������������������������sleeping������������������������������������������������������������������������������������������������������������������
- ������������������������������������������������������������������������
- ������������Running������������������������������������������������
- ������������Blocking������������������������I/O������������������������������������������
- ������������Waiting���������������������������������������
- ���������������Turnaround waiting���������������������������
- ���������������Terminated���������������������������
������������������������������������������������������������������������������������������������������������������������
sleep������������
������������������������������������������������Thread.sleep()
���������������������������������������������������������������notify()
���notifyAll()
������I/O���������������������������������������
t.sleep(500); // ���������������500������
sleep()
���������������������������������������������������������������������������������������������������������������
join������������������
Thread.join()
���������������������������������������������������������������������sleep()
���������������join()
���������������������������������������������������������������������������������������������������������
t.join();
join()
������������������������������������������������������������������������������������IllegalStateException
���
���������������������
���������������������������Thread
���������������������������������1���10���10������������������������������������Comparable
���������Thread
������������������������������������
t.setPriority(10);
������������������������������������������������������������������������������CPU���������
������������������������
���������������������������������������������������������������������������������������������������Java���������synchronize
������������������������������������������������������
public class SafeResource { private int count = 0; public synchronized void increment() { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } count++; } public synchronized void���������������() { while (count < 100) { System.out.println("������������... " + count); increment(); } }}
������synchronized
������������������������������������������������������������������������������������������������������
wait���notify������
������������������������������������������������������������wait()
���notify()
���������wait()
���������������������������������������������������notify()
���notifyAll()
������������
public class RetryPattern { private boolean isRunning = false; public void processRequest(final String input) { isRunning = true; Runnable task = () -> { try { // ��������������������������������������������������� System.out.println("���������������������" + input); Thread.sleep(100); done(); } catch (InterruptedException e) { interrupted(); } }; new Thread(task).start(); System.out.println("������������������������" + input); } public void done() { synchronized (this) { isRunning = false; notifyAll(); } System.out.println("���������������������" + input); } // ������������������������������������}
wait()
���notifyAll()
������������������������������������������������������������������������
������
���������������������������������������Java���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
