
本文共 20076 字,大约阅读时间需要 66 分钟。
������������������
������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������QQ���������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������
1.2 ���������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������CPU���
1.3 ���������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
1.4 ���������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Response Time������������������Throughput���������������������QPS���Query Per Second���������������������������
������������������������������/���������������������������������������������������������������������������������������������������������������������������������
1.5 ������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������,���������������������������������������������������������������������������������
������������������������������
���������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������
Java���������������������������������������
���������������������
���������������������������������������������������������������������������������������������,������������������������������������
2.1 ������Thread���������
������Thread������������������������������������������
������������������Thread,������run()������,run()���������������������������������������������
���������������������������������������������������
���������������start()���������������������
public class FirstThread extends Thread { private int i=0; public void run() { for (; i < 100; i++) { //������������������������ System.out.println(this.getName() + " " + i); } } public static void main(String[] args) { for (int i = 0; i < 100; i++) { //Thread���������������currentThread��������������������� System.out.println(Thread.currentThread().getName()); if (i == 20) { //��������������������� new FirstThread().start(); new FirstThread().start(); } } }}
���������������������������������������i������������������������������������������������������
2.2 ������Runnable������
������Runnable���������������������������������������������������
������������������Runnable������,������run()������,run()���������������������������������������������
���������������������������������������������target������������������Thread���������������Thread���������������������������������������
public class SecondThread implements Runnable { private int i = 0; @Override public void run() { for (; i < 100; i++) { //���������������������������������������������������Thread.currentThread()������ System.out.println(Thread.currentThread().getName() + " " + i); } } public static void main(String[] args) { for (int i = 0; i < 100; i++) { //Thread���������������currentThread��������������������� System.out.println(Thread.currentThread().getName()); if (i == 20) { //��������������������� SecondThread secondThread=new SecondThread(); new Thread(secondThread,"���������").start(); new Thread(secondThread,"���������").start(); } } }}
2.3 ������Callable���Future
Callable���Runnable������������������������������������call()������������������������������������������������������������������Callable������������������������
������������������Callable������,������call()������,run()���������������������������������������������
������������������������FutureTask������������������������
������FutureTask������������Thread���target���������������������������������������
������FutureTask���������get()������������������������������������������������
public class ThirdThread { public static void main(String[] args) { //������lambda��������� FutureTasktask = new FutureTask<>((Callable ) () -> { int i = 0; for (; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "���������������i���������" + i); } return i; }); for (int i = 0; i < 100; i++) { //Thread���������������currentThread��������������������� System.out.println(Thread.currentThread().getName()); if (i == 20) { //��������������������� new Thread(task, "���������������������").start(); } } try { System.out.println("���������������������" + task.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }}
���������������lambda���������������������������������������������������������������������������������Callable���Runnable���������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������Thread������������������������������������������
������������������:
- ���������������������������������������������������������������������������������������������������target���������������������������������������������������
- ������������������������������������������������������������������������������������������������������Thread.currentThread()���������
������Thread���:
���������������������
������������������������������
���������������������
3.1 ������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������(NEW)������������������������������������������������������start()������������������������������������������������������������������������
������(RUNNABLE)������������������������������������RUNNABLE���������
������(BLOCKED)������������������������������������������������������������
������(WAITING)���������������������������������������������������������������������������������������������������
������������(TIMED_WAITING)���������������������WAITING������������������������������������������������
������(TERMINATED)���������������������������������������
������������������������
���������������������������������������������������������������������������������������������������������������������������start()������������������������������������������������������������������������������
������������������������������������������������������RUNING���������������READY������
���������������������������������������������CPU������������������������������������������������������������������������������������������������
���������������start()���������
������������sleep()���������������������������join()������������������������������������������������������������������
������������������������������������������������������yield()���������
���������������������������������������
������������RUNING������������������������������������������������������������������������
������������������������������������synchronized������������������������������������(���������)���������������
���������������������������������CPU������������������������������������������������������������������������������������������������������������
���������������������������������������CPU���������������������������������������������������������������������������������������������������������������zi������������
������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������break������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������stop()������������������������������������������������������������������������������������������������������������������������interrupted���
interrupted������������������������������������������������������������������������������������������������������true,���������������������������������������������������������������������������������
public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 50000; i++) { System.out.println("i=" + (i + 1)); } } public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } }}
���������������������������5���������������
Thread������������������������������������������������
static boolean interrupted()���������������������������������������������������������
boolean isInterrupted()������������������������������������������������������
Thread.currentThread().interrupt();System.out.println("������������1���=" + Thread.interrupted());//true���������������������������������System.out.println("������������2���=" + Thread.interrupted());//false
������������interrupted������������������������������������������������������������������������������������������������������������������������������������������������������������
public class FiveThread extends Thread {@Overridepublic void run() { for (int i = 0; i < 500000; i++) { if (this.isInterrupted()) { System.out.println("������������������������!������!"); break; } System.out.println("i=" + (i + 1)); } System.out.println("666");}public static void main(String[] args) { try { FiveThread thread = new FiveThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!");}}
���������������������
������������������������������for������������������for���������������������������������������������������������������������������������������������������������������������
@Overridepublic void run() { try { for (int i = 0; i < 500000; i++) { if (this.isInterrupted()) { System.out.println("������������������������!������!"); throw new InterruptedException(); } System.out.println("i=" + (i + 1)); } } catch (InterruptedException e) { e.printStackTrace(); System.out.println("������������������"); } System.out.println("666");}
���������������������������return������������������������������������������������������������������������������������������������������
public class JoinThread extends Thread {private Thread thread;public JoinThread(Thread thread) { this.thread = thread;}@Overridepublic void run() { try { thread.join(); for (int i = 0; i < 10; i++) { System.out.println(thread.getName() + "��������� " + i); } } catch (InterruptedException e) { e.printStackTrace(); }}public static void main(String[] args) { Thread previousThread = Thread.currentThread(); for (int i = 1; i <= 10; i++) { Thread curThread = new JoinThread(previousThread); curThread.start(); previousThread = curThread; }}}
������������������������������������10���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
join������������������������
join()������������join������������������������
join(long millis)������������join���������millis���������������������������������������������������������
join(long millis,int nanos)������������join���������millis���������nanos���������
3.4 sleep
���������������������������������������������������������������������������������sleep���������������������Thread������������������������������������������������������sleep���������������������������������������������������������������������������������sleep������������������������sleep���������������������Object.wait()���������������������
������������������������
sleep()���������Thread���������������������wait���Object������������
wait()���������������������������������������������������������������������������������������������������sleep()������������������������������������������������������������������wait()���������������������������������������������������������������������������������������������������������sleep()���������������������CPU������������������������������
sleep()������������������������������������������������CPU���������������������������������wait()������������������Object.notift/Object.notifyAll������������������������������������������������������CPU������������������������������
3.5 yield
yield���������������������������������������������������������������������CPU������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������CPU������������������������������������������������������������������������������������������������������������������������������
public class YieldThread extends Thread { public YieldThread(String name) { super(name); } @Override public void run() { for (int i = 0; i < 50; i++) { //������������������������ System.out.println(this.getName() + " " + i); if (i == 20) { Thread.yield(); } } } public static void main(String[] args) { YieldThread yieldThread = new YieldThread("������"); //yieldThread.setPriority(Thread.MAX_PRIORITY); yieldThread.start(); YieldThread yieldThread2 = new YieldThread("������"); //yieldThread2.setPriority(Thread.MIN_PRIORITY); yieldThread2.start(); }}
���������������������������������������������������������20������������������������������������������������������������������������������������������������yield������������������������������������������������������������������������������������������
���������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������main���������������������������������������������������������������������������
setPriority������������������������������������������1-10���������������������������������������������
MAX_PRIORITY���������10
MIN_PRIORITY���������1
NORM_PRIORITY���������5
������������������������������������������������cpu������������������������������������������������������������������������������������������������������������run���������
������������������
Java������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Java���������������������������������������������������������������������
public class DaemonThread extends Thread { @Override public void run() { while (true) { try { System.out.println("i am alive"); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println("finally block"); } } } public static void main(String[] args) { DaemonThread daemonThread = new DaemonThread(); daemonThread.setDaemon(true); daemonThread.start(); //������main���������������������daemonThread��������������������� try { Thread.sleep(800); } catch (InterruptedException e) { e.printStackTrace(); } }}
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������finnaly������������������������������������������������������������finnaly���������������������������������������������
������������������setDaemon(true)���������������������������������������������������������������������������������������������start()���������
发表评论
最新留言
关于作者
