李海成 2019-04-04 11:48 采纳率: 33.3%
浏览 1341

如何将两个线程绑定到同一个cpu? Java-Thread-Affinity能否实现?

1.需求,现在需要将两个线程绑定到同一个cpu运行。
2.进度,现在查到 Java-Thread-Affinity工具是最接近这种需求的,但是感觉 Java-Thread-Affinity只能让某个线程独占cpu,。所以, Java-Thread-Affinity能实现这种需求吗?
3.代码

package net.openhft.affinity;

import static net.openhft.affinity.AffinityStrategies.*;
public final class AffinityLockBindMain {
    private AffinityLockBindMain() {
        throw new InstantiationError("Must not instantiate this class");
    }

    public static void main(String... args) throws InterruptedException {
        AffinityLock al = AffinityLock.acquireLock();
        try {
            // find a cpu on a different socket, otherwise a different core.
            System.out.println("al id " + al.cpuId());
            AffinityLock readerLock = al.acquireLock(SAME_CORE, SAME_SOCKET);
            System.out.println("reader id " + readerLock.cpuId());
            new Thread(new SleepRunnable(readerLock, true), "reader").start();

            // find a cpu on the same core, or the same socket, or any free cpu.
            //AffinityLock writerLock = al.acquireLock(SAME_CORE, SAME_SOCKET);
            System.out.println("writerLock id " + readerLock.cpuId());
            new Thread(new SleepRunnable(readerLock, true), "writer").start();

            Thread.sleep(200);
        } finally {
            al.release();
        }

        // allocate a whole core to the engine so it doesn't have to compete for resources.
        al = AffinityLock.acquireCore(false);
        System.out.println("engine id " + al.cpuId());
        new Thread(new SleepRunnable(al, true), "engine").start();

        Thread.sleep(200);
        System.out.println("\nThe assignment of CPUs is\n" + AffinityLock.dumpLocks());
    }

    static class SleepRunnable implements Runnable {
        private final AffinityLock affinityLock;
        private final boolean wholeCore;

        SleepRunnable(AffinityLock affinityLock, boolean wholeCore) {
            this.affinityLock = affinityLock;
            this.wholeCore = wholeCore;
        }

        public void run() {
            affinityLock.bind(wholeCore);
            try {
                Thread.sleep(1000);
                Thread t = Thread.currentThread();
                String name = t.getName();
                System.out.println("name=" + name);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                affinityLock.release();
            }
        }
    }
}

4.报错
Exception in thread "writer" java.lang.IllegalStateException: cpu 6 already bound to Thread[reader,5,main]
at net.openhft.affinity.AffinityLock.bind(AffinityLock.java:240)
at net.openhft.affinity.AffinityLockBindMain$SleepRunnable.run(AffinityLockBindMain.java:68)
at java.lang.Thread.run(Thread.java:748)

  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2019-04-04 16:48
    关注

    线程的调度是操作系统级别的,应该不能控制的吧。

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)