Java中的线程(Threads in Java)
我今天在一次采访中被问到Java中的Thread概念? 问题是......
- 什么是线程?
- 我们为什么要穿线?
- 线程上的实时示例。
- 我们可以在Spring框架服务类中创建线程吗?
- 可以弹性调用线程?
除了Thread的定义之外,我没有回答任何问题,我也是从互联网上学到的。
任何人都可以解释清楚这一点。
更新 :
线程和普通Java类有什么区别? 为什么我们需要线程...我可以在线程中执行业务逻辑吗? 我可以在线程中调用不同的类方法吗?
I was today asked in an interview over the Thread concepts in Java? The Questions were...
- What is a thread?
- Why do we go for threading?
- A real time example over the threads.
- Can we create threads in Spring framework service class.
- 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
满意答案
它不是#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; };
相关问答
更多在Screeps中,是否强制执行CPU限制以允许写入CPU限制健壮代码?(In Screeps, is CPU limit enforced in a way that allows CPU limit robust code to be written?)
如何限制进程在Windows上的CPU使用率?(How do you limit a process' CPU usage on Windows? (need code, not an app))
Python - 限制脚本的CPU使用率(Python - Limit cpu usage for a script)
限制python多处理中的总CPU使用率(Limit total CPU usage in python multiprocessing)
多处理 - 限制CPU使用率(Multiprocessing - limit CPU usage)
限制CPU使用率的线程数(Number of threads to limit CPU usage)
限制C ++中的CPU使用率(Limit CPU Usage in C++)
限制SqlBulkCopy的CPU(Limit CPU at SqlBulkCopy)
是否有任何限制mongodb CPU使用?(Is there anything to limit mongodb cpu usage?)
限制Windows .NET服务的CPU使用率(Limit the cpu usage of a windows .NET service)
相关文章
更多java线程状态详解(6种)
Java 多线程编程
请教多线程JAVA问题
求推荐JAVA 多线程编程书籍
Callable接口实现Java多线程
一步一步掌握java的线程机制(一)----创建线程
在main方法中开启线程与@Test中开启线程
java上传
Java 简介
Java 异常处理
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!