栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

java之线程交替执行(三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java之线程交替执行(三)

LockSupport:

package com.company;

import java.util.concurrent.locks.LockSupport;

public class Main {

    static Thread t1, t2;

    public static void main(String[] args) throws Exception {
        char[] ca1 = "abcdefg".toCharArray();
        char[] ca2 = "1234567".toCharArray();

        t2 = new Thread(() -> {
            for (char ch : ca2) {
                LockSupport.park();
                System.out.println(ch);
                LockSupport.unpark(t1);
            }

        });

        t1 = new Thread(() -> {
            for (char ch : ca1) {
                System.out.println(ch);
                LockSupport.unpark(t2);
                LockSupport.park();
            }

        });

        t1.start();
        t2.start();
    }

}

结果:

a
1
b
2
c
3
d
4
e
5
f
6
g
7

wait/notify:

package com.company;

public class Main {

    static Thread t1, t2;

    public static void main(String[] args) throws Exception {
        char[] ca1 = "abcdefg".toCharArray();
        char[] ca2 = "1234567".toCharArray();
        String object = "lock";

        t2 = new Thread(() -> {
            for (char ch : ca2) {
                synchronized (object) {
                    object.notify();
                    System.out.println(ch);
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("t2 end");
        });

        t1 = new Thread(() -> {
            for (char ch : ca1) {
                synchronized (object) {
                    System.out.println(ch);
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    object.notify();
                }
            }

            System.out.println("t1 end");
        });

        t1.start();
        t2.start();
    }

}

Condition:

package com.company;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

    static Thread t1, t2;

    public static void main(String[] args) throws Exception {
        char[] ca1 = "abcdefg".toCharArray();
        char[] ca2 = "1234567".toCharArray();

        ReentrantLock reentrantLock = new ReentrantLock();
        Condition c1 = reentrantLock.newCondition();
        Condition c2 = reentrantLock.newCondition();

        t2 = new Thread(() -> {
            for (char ch : ca2) {
                try {
                    reentrantLock.lock();
                    c2.signal();
                    System.out.println(ch);
                    try {
                        c1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } finally {
                    reentrantLock.unlock();
                }

            }
            System.out.println("t2 end");
        });

        t1 = new Thread(() -> {
            for (char ch : ca1) {
                try {
                    reentrantLock.lock();

                    try {
                        c2.await();
                        System.out.println(ch);
                    } catch (InterruptedException e) {
                     e.printStackTrace();
                    }
                    c1.signal();
                } finally {
                    reentrantLock.unlock();
                }
            }

            System.out.println("t1 end");
        });

        t1.start();
        t2.start();
    }

}

TransferQueue:

package com.company;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

public class Main {

    static Thread t1, t2;

    public static void main(String[] args) throws Exception {
        char[] ca1 = "abcdefg".toCharArray();
        char[] ca2 = "1234567".toCharArray();

        TransferQueue queue = new LinkedTransferQueue<>();

        t2 = new Thread(() -> {
            for (char ch : ca2) {
                try {
                    queue.transfer(ch);
                    System.out.println(queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t2 end");
        });

        t1 = new Thread(() -> {
            for (char ch : ca1) {
                try {
                    System.out.println(queue.take());
                    queue.transfer(ch);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("t1 end");
        });

        t1.start();
        t2.start();
    }

}

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号