多线程:我们如何使用 Java 进行时间分片?

Multithreading: How can we do Time Slicing using Java?

是否可以使用 Java 使用 In-bult java 类 进行时间分片?

在互联网上搜索时,我只得到以下定义:

Time slicing is a term which is usually associated with the processor and operating system ... what it means is ... When you have many processes running by an operating system then each process has to get a chance to run ..that is each of the process should get the processor for particular time.....so now if you have 3 processes running eg- p0,p1,p2 then now p0 can run for a time of 5 ms then comes turn for p1 then for p2..... thus you are giving some time called as timeslice to each of the process that has to run by the processor..... Thus there are different algorithms which exists for these processes(depeneds on the operating System) to run on a processor.....

我在 Java 面试问题中得到了这个问题。但无法获得任何 Java 与之相关的代码示例。

时间切片仅仅是操作系统的概念还是有任何实际用法可以显示使用 Java 编程?有人可以分享相同的示例吗?

int n = Runtime.getRuntime().availableProcessors(); 然后启动 n + 1 个线程。根据定义,如果 n + 1 个线程在 n 个内核上 运行ning,那么就会发生时间分片。实际使用,需要运行n + 1个线程,只有ncpu个内核。