JVM在什么基础上为任务分配线程?提供了一个我在这里试过的例子[复制]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JVM在什么基础上为任务分配线程?提供了一个我在这里试过的例子[复制]相关的知识,希望对你有一定的参考价值。

//这个类包含任务。我做这个练习来理解多线程。我试图获取线程的名称,因为我想看看它们是如何被执行的,即,Thread-0进入块但是当调用sleep()方法时,Thread-1首先进入休眠状态。我不确定这是怎么发生的,因为如果Thread-0先进入,那么Thread-0必须先睡觉才对吗?此外,我的任务在synchronized块中执行,因此在Thread-0完成执行之前,Thread-1如何进入synchronized块。

我已经在下面发布了输出。

    class Runner extends Thread {
        private Object obj = new Object();

        @Override
        public void run() {
            String[] someValues = { "Arun", "Kumar", "is", "an", "amazing", "person" };
            synchronized (obj) {
                System.out.println("Current thread entering synchronized block is " + Thread.currentThread().getName());
                for (String getSomeValues : someValues) {
                    System.out.println("Current thread printing the value is " + Thread.currentThread().getName());
                    System.out.print(getSomeValues + " ");
                    try {
                        // Sleep pauses the program.
                        // It also throws InterruptedException so it must be
                        // handled.
                        System.out.println("\nThread BeforeSleep is " + Thread.currentThread().getName());
                        Thread.sleep(100);
                        System.out.println("\nThread AfterSleep is " + Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

//主App类执行线程。

    public class App {
        public static void main(String[] args) {
            Runner r1 = new Runner();
            // The start() method starts the run method in Runner class.
            r1.start();
            Runner r2 = new Runner();
            r2.start();
        }
    }

//输出代码//开始输出

进入同步块的当前线程是Thread-0当前线程打印值为Thread-0当前线程进入同步块是Thread-1当前线程打印值为Thread-1 Arun Thread BeforeSleep是Thread-1 Arun Thread BeforeSleep是Thread-0

线程AfterSleep是Thread-0

线程AfterSleep是Thread-1当前线程打印的值是Thread-0 Kumar当前线程打印的值是Thread-1

Thread BeforeSleep是Thread-0 Kumar Thread BeforeSleep是Thread-1

线程AfterSleep是Thread-0

线程AfterSleep是Thread-1当前线程打印的值是Thread-1当前线程打印的值是Thread-0是Thread BeforeSleep是Thread-1是Thread BeforeSleep是Thread-0

线程AfterSleep是Thread-1

线程AfterSleep是Thread-0当前线程打印的值是Thread-1当前线程打印的值是Thread-0一个线程BeforeSleep是Thread-0

Thread BeforeSleep是Thread-1

线程AfterSleep是Thread-0当前线程打印的值是Thread-0

线程AfterSleep是Thread-1当前线程打印的值是Thread-1惊人的惊人线程BeforeSleep是Thread-1

Thread BeforeSleep是Thread-0

线程AfterSleep是Thread-1当前线程打印的值是Thread-1人Thread BeforeSleep是Thread-1

线程AfterSleep是Thread-0当前线程打印的值是Thread-0 person Thread BeforeSleep是Thread-0

线程AfterSleep是Thread-1

线程AfterSleep是Thread-0

//结束输出

答案

实际上,由于Java Thread synchronization - Thread.sleep() Method Not Working as desired ,我想出了一个简洁的方法

我改变了我的主类看起来像这样

public class App {
    public static void main(String[] args) {
        Object obj = new Object();
        Runner r1 = new Runner(obj);
        // The start() method starts the run method in Runner class.
        r1.start();
        Runner r2 = new Runner(obj);
        r2.start();
    }
}

Runner类将从构造函数中获取对象,而不是创建新实例。因此,Synchronize更成功。

以上是关于JVM在什么基础上为任务分配线程?提供了一个我在这里试过的例子[复制]的主要内容,如果未能解决你的问题,请参考以下文章

在Android上为MediaStreamer2控制台应用程序提供JVM

java守护线程

多线程基础学习

JVM系列 - JVM垃圾回收算法

20200225 Java 多线程-廖雪峰

理解JVM线程与Java的线程