简体   繁体   English

java 线程和 OS 线程

[英]java threads and OS threads

Some threads such as this one say that the threads I can run on my machine is 4. Does this mean if I created a multi-threaded java program, I can create only 2 x 2 x 1 = 4 threads without any blocking or performance issues?像这个这样的一些线程说我可以在我的机器上运行的线程是 4。这是否意味着如果我创建一个多线程 java 程序,我只能创建2 x 2 x 1 = 4线程而没有任何阻塞或性能问题?

CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  2
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               69
Model name:          Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz
Stepping:            1
CPU MHz:             926.041
CPU max MHz:         3000.0000
CPU min MHz:         800.0000
BogoMIPS:            4788.55
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            4096K
NUMA node0 CPU(s):   0-3
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d

You can run as many threads as you want;您可以运行任意数量的线程; you are limited only by your memory (as each thread needs a little memory for tracking purposes; primarily, the stack size).您仅受 memory 的限制(因为每个线程都需要一点 memory 用于跟踪目的;主要是堆栈大小)。 A thousand threads is generally no problem, for example.例如,一千个线程通常是没有问题的。

That output suggests that at any given time, 4 of your threads will actually be actively processing statements and all others will be waiting around. output 表明在任何给定时间,您的 4 个线程实际上将在积极处理语句,而所有其他线程将等待。 Note that threads are 'pre-emptive', meaning, they run for a bit and are then automatically frozen so that another one can have a turn.请注意,线程是“先发制人的”,这意味着它们会运行一段时间,然后自动冻结,以便另一个线程可以轮流。 Furthermore, if a thread 'blocks' for any reason (blocks = doing something that needs to wait for something to finish, and it's not the CPU, then other threads get their turn right away.此外,如果一个线程因任何原因“阻塞”(阻塞 = 做一些需要等待某事完成的事情,而不是 CPU,那么其他线程会立即轮到他们。

Think of:考虑到:

  • Reading files (waits for the disk)读取文件(等待磁盘)
  • Sending bytes on a network connection (wait for the bytes to get through to the network card buffer / waiting for that buffer to have room)在网络连接上发送字节(等待字节通过网卡缓冲区/等待该缓冲区有空间)
  • Sending audio wave data to the soundcard将音频波数据发送到声卡
  • Just 'wait for some seconds' instructions只需“等待几秒钟”的说明

The short of it?不足之处? Just make as many threads as you want, and treat them as if they all run simultaneously.只需创建任意数量的线程,并将它们视为同时运行。 There's no reason to worry about performance or if this is the right approach until you get to over a thousand of them.没有理由担心性能,或者这是否是正确的方法,直到你达到一千多个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM