首页 \ 问答 \ Java中的线程(Threads in Java)

Java中的线程(Threads in Java)

我今天在一次采访中被问到Java中的Thread概念? 问题是......

  1. 什么是线程?
  2. 我们为什么要穿线?
  3. 线程上的实时示例。
  4. 我们可以在Spring框架服务类中创建线程吗?
  5. 可以弹性调用线程?

除了Thread的定义之外,我没有回答任何问题,我也是从互联网上学到的。

任何人都可以解释清楚这一点。

更新

线程和普通Java类有什么区别? 为什么我们需要线程...我可以在线程中执行业务逻辑吗? 我可以在线程中调用不同的类方法吗?


I was today asked in an interview over the Thread concepts in Java? The Questions were...

  1. What is a thread?
  2. Why do we go for threading?
  3. A real time example over the threads.
  4. Can we create threads in Spring framework service class.
  5. Can flex call a thread?

I did not answer any questions apart from definition of Thread, that too I just learnt from internet.

Can anyone explain me clearly over this.

Update:

What is a difference between a thread and a normal java class. why do we need threading... can i execute business logic in threads. Can i call a different class methods in Threads.


原文:https://stackoverflow.com/questions/2865315
更新时间:2023-05-15 06:05

最满意答案

它不是#1或#2。 我打赌它是#4,最有意义的是监视主循环外部的CPU使用情况,并在达到限制时将其杀死。 #3需要在screeps服务器上执行“语句级”事务的复杂代码。 正如你发现的那样,模拟器没有CPU限制。

大多数玩家通过简单地在主循环中尽早放置关键代码来解决这个问题,例如塔码先出现,然后产生代码,然后蠕动移动/工作。 这也可以防止代码中的未捕获异常,因为最重要的功能将(希望)已经执行。 尽管这对于CPU限制来说是一个糟糕的解决方案,但在我看来,一旦使用了存储桶中的所有CPU并且不断达到常规限制,您的代码就会像跳过每第二个tick一样。

我现在没有CPU问题(我有一个订阅),但是我可以通过将CPU密集型代码放到最后来解决这个问题,并且如果可能的话,只有在您的存储桶中有足够的CPU时才执行它,距离你的500个CPU每刻度限制不远。 它也有助于产生更大的毛刺,对于寻找或者甚至正好运动(每次移动0.2次)占用相当一部分CPU以及更大的毛刺意味着更少的毛刺是很常见的。


Case 4 by default, but modifiable to case 2.5

As nehegeb and dwurf suspected, and experiments with a private server have confirmed, the default behaviour is case 4. Changes to both game state and Memory that occurred before the interruption are committed.

However, the running of the default JSON serialize by the server main loop is controlled by the existence of an undocumented key '_parsed' in RawMemory; the key's value is a reference to Memory. Deleting the key at the start of the script's main loop and restoring it at the end has the effect of making the whole set of Memory changes made by the script's main loop atomic i.e. case 2.5:

module.exports.loop = function()
{
  // Run the default JSON deserialize. This also creates a key '_parsed' in
  // RawMemory - that '_parsed' key and Memory refer to the same object, and the
  // existence of the '_parsed' key tells the server main loop to run the
  // default JSON serialize.
  Memory;

  // Disable the default JSON serialize by deleting the key that tells the
  // server main loop to run it.
  delete RawMemory._parsed;

  ...

  // An example of code that would be wrong without a way to make it CPU limit
  // robust:

  mySpawn.memory.queue.push('harvester');
  // If the script is interrupted here, myRoom.memory.harvesterCreepsQueued is
  // no longer an accurate count of the number of 'harvester's in
  // mySpawn.memory.queue.
  myRoom.memory.harvesterCreepsQueued++;

  ...

  // Re-enable the default JSON serialize by restoring the key that tells the
  // server main loop to run it.
  RawMemory._parsed = Memory;
};

相关问答

更多
  • 它不是#1或#2。 我打赌它是#4,最有意义的是监视主循环外部的CPU使用情况,并在达到限制时将其杀死。 #3需要在screeps服务器上执行“语句级”事务的复杂代码。 正如你发现的那样,模拟器没有CPU限制。 大多数玩家通过简单地在主循环中尽早放置关键代码来解决这个问题,例如塔码先出现,然后产生代码,然后蠕动移动/工作。 这也可以防止代码中的未捕获异常,因为最重要的功能将(希望)已经执行。 尽管这对于CPU限制来说是一个糟糕的解决方案,但在我看来,一旦使用了存储桶中的所有CPU并且不断达到常规限制,您的代 ...
  • 非常简单,它可以像这样工作: 用一些合理的小等待时间(可能是100ms)创建一个定期等待定时器。 通过调用一次GetProcessTimes为每个相关进程获取“最后”值。 永远循环,阻止定时器。 每次你醒来: 如果 GetProcessAffinityMask返回0,则调用SetProcessAffinityMask(old_value) 。 这意味着我们在上一次迭代中暂停了这一过程,现在我们给它一次再次运行的机会。 否则调用GetProcessTimes来获取“当前”值 调用GetSystemTimeAs ...
  • 也许尝试Python的“ 资源 ”包? 资源包基本上是UNIX中“setrlimit”的概括。 所以你可以设置CPU /内存使用限制等。 有关UNIX中的“setrlimit”的更多信息 谢谢@Stephane指出,这实际上是我的第一个答案。 Maybe try Python's "resource" package ? The resource package is basically a wraparound of "setrlimit" in UNIX. So you can set constrai ...
  • 解决方案取决于你想要做什么。 这里有几个选项: 过程的优先级较低 你可以nice的子过程。 这样,尽管他们仍然会吃掉100%的CPU,但当您启动其他应用程序时,操作系统会优先考虑其他应用程序。 如果您想在笔记本电脑的背景上运行大量计算工作,并且不关心CPU风扇一直在运行,那么使用psutils设置nice值是您的解决方案。 这个脚本是一个测试脚本,它可以在所有内核上运行足够的时间,这样你就可以看到它的行为。 from multiprocessing import Pool, cpu_count import ...
  • 考虑使用multiprocessing.pool.ThreadPool 。 它提供与multiprocessing.Pool相同的API,但将工作负载抽象为线程集合。 请注意,如果您的CPU支持超线程,那么它很可能会通过物理内核分配工作负载。 Consider using multiprocessing.pool.ThreadPool. It provides the same API as multiprocessing.Pool but abstracts the workload to a colle ...
  • CPU使用率为50%并不一定意味着线程数需要为N_Cores / 2.在处理I / O时,CPU在等待数据从设备到达时会浪费很多周期。 因此,您需要一个工具来测量实际CPU使用率,并且通过实验,您可以增加线程数,直到实际CPU使用率达到50%。 针对Linux的perf就是这样一个工具。 这个问题解决了这个问题。 另外一定要收集统计系统: perf记录-a 。 您感兴趣的是CPU发出并执行尽可能多的指令/循环 (IPC)。 现代服务器可以执行多达4个IPC,用于强大的计算绑定工作负载。 您希望尽可能接近以获 ...
  • 使用getrusage() ,请参阅Linux命令以检测程序的计算机资源使用情况 当你检查,并且你已经使用了你想要的毫秒运行时间时,使用nanosleep()来休眠几毫秒。 调整百分比以符合您的要求。 Use getrusage(), see Linux commands to detect the computer resource usage of a program And when you check, and you've used however many milliseconds of run ...
  • 您正在更新的数据是否会影响其他计算机查询的结果? 如果它然后它应该阻止它们,与CPU无关,但因为记录被锁定以进行更新。 您希望操作尽可能快地运行,因此最大CPU是好的。 如果这两个数据集不同,那么没有理由为配置良好的SQL服务器无法同时服务这两个数据集,请查看调优选项,可用内存量等 Is the data you're updating affecting the results of the other machine's queries? If it is then it should block th ...
  • 你很可能缺少索引; 请确保查看PHP教程的索引部分。 You most likely are missing indexes; make sure you review the indexing section of the PHP tutorial.
  • 这些天的内存带宽和处理器缓存的使用与问题的匹配是CPU时间。 我不知道一个跟不上的操作系统...... 您可以尝试使用Thread.Sleep睡眠时间的50%,凌乱但可能有效。 These days’ memory band-width and usage of the processor cache is as match of a problem is CPU time. I don’t know of a OS that has kept up... You could try using a Thr ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)