Java多线程
发布日期:2021-05-10 11:49:04 浏览次数:7 分类:精选文章

本文共 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���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Java输入输出流
    下一篇:Python+selenium破解PTA滑块验证码登录,爬取题目和答案

    发表评论

    最新留言

    不错!
    [***.144.177.141]2025年03月30日 19时40分35秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章