多线程与高并发(一)多线程基础
发布日期:2021-05-09 09:07:34 浏览次数:15 分类:博客文章

本文共 20076 字,大约阅读时间需要 66 分钟。

������������������

������������������������������������������������������������������������������������������������������������

1.1 ���������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������QQ���������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������

1.2 ���������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������CPU���

1.3 ���������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

1.4 ���������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Response Time������������������Throughput���������������������QPS���Query Per Second���������������������������

������������������������������/���������������������������������������������������������������������������������������������������������������������������������

1.5 ������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������,���������������������������������������������������������������������������������

������������������������������

  • ���������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������

  • Java���������������������������������������

���������������������

���������������������������������������������������������������������������������������������,������������������������������������

2.1 ������Thread���������

������Thread������������������������������������������

  1. ������������������Thread,������run()������,run()���������������������������������������������

  2. ���������������������������������������������������

  3. ���������������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���������������������������������������������������

  1. ������������������Runnable������,������run()������,run()���������������������������������������������

  2. ���������������������������������������������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������������������������

  1. ������������������Callable������,������call()������,run()���������������������������������������������

  2. ������������������������FutureTask������������������������

  3. ������FutureTask������������Thread���target���������������������������������������

  4. ������FutureTask���������get()������������������������������������������������

public class ThirdThread {    public static void main(String[] args) {        //������lambda���������        FutureTask
task = 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 ������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  1. ������(NEW)������������������������������������������������������start()������������������������������������������������������������������������

  2. ������(RUNNABLE)������������������������������������RUNNABLE���������

  3. ������(BLOCKED)������������������������������������������������������������

  4. ������(WAITING)���������������������������������������������������������������������������������������������������

  5. ������������(TIMED_WAITING)���������������������WAITING������������������������������������������������

  6. ������(TERMINATED)���������������������������������������

������������������������

���������������������������������������������������������������������������������������������������������������������������start()������������������������������������������������������������������������������

������������������������������������������������������RUNING���������������READY������

���������������������������������������������CPU������������������������������������������������������������������������������������������������

  • ���������������start()���������

  • ������������sleep()���������������������������join()������������������������������������������������������������������

  • ������������������������������������������������������yield()���������

  • ���������������������������������������

������������RUNING������������������������������������������������������������������������

������������������������������������synchronized������������������������������������(���������)���������������

���������������������������������CPU������������������������������������������������������������������������������������������������������������

���������������������������������������CPU���������������������������������������������������������������������������������������������������������������zi������������

������������������������������������������������������������������������������������������������������������������������������������������

3.2 interrupted

���������������������������������������������������������������������������������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������������������������������������������������������������������������������������������������������

3.3 join

join������������������������������������������������������������������������������������������������������������������������������������������������������������������join������������������������������������������������join������������������������������������

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()���������������������

������������������������

  1. sleep()���������Thread���������������������wait���Object������������

  2. wait()���������������������������������������������������������������������������������������������������sleep()������������������������������������������������������������������wait()���������������������������������������������������������������������������������������������������������sleep()���������������������CPU������������������������������

  3. 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()���������

上一篇:多线程与高并发(二)线程安全
下一篇:Java核心技术梳理-类加载机制与反射

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月09日 16时59分42秒