ThreadLocal是如何实现的?它是用Java实现的(使用从ThreadID到对象的一些并发映射),还是使用一些JVM钩子来更有效地执行它?
我想在我的shell脚本中介绍多线程功能.
我有一个脚本,read_cfg()用不同的参数调用该函数.每个函数调用都是独立的.
是否可以并行地实例化这些函数调用(而不是脚本).请让我怎样才能实现这一目标..?
如果我有一个无限循环的线程,有没有办法在主程序结束时终止它(例如,当我按Ctrl+ C)?
我正在使用此代码对ReaderWriterLock进行非常愚蠢的基准测试,其中读取的次数比写入次数多4倍:
class Program
{
static void Main()
{
ISynchro[] test = { new Locked(), new RWLocked() };
Stopwatch sw = new Stopwatch();
foreach ( var isynchro in test )
{
sw.Reset();
sw.Start();
Thread w1 = new Thread( new ParameterizedThreadStart( WriteThread ) );
w1.Start( isynchro );
Thread w2 = new Thread( new ParameterizedThreadStart( WriteThread ) );
w2.Start( isynchro );
Thread r1 = new Thread( new ParameterizedThreadStart( ReadThread ) );
r1.Start( isynchro );
Thread r2 = new Thread( new ParameterizedThreadStart( ReadThread ) …Run Code Online (Sandbox Code Playgroud) 我刚刚在这篇博客文章中找到了CompletionService .但是,这并没有真正展示CompletionService相对于标准ExecutorService的优势.可以用任何一个编写相同的代码.那么,什么时候CompletionService有用呢?
你能给一个简短的代码样本,使它清晰吗?例如,此代码示例仅显示不需要CompletionService的位置(=等效于ExecutorService)
ExecutorService taskExecutor = Executors.newCachedThreadPool();
// CompletionService<Long> taskCompletionService =
// new ExecutorCompletionService<Long>(taskExecutor);
Callable<Long> callable = new Callable<Long>() {
@Override
public Long call() throws Exception {
return 1L;
}
};
Future<Long> future = // taskCompletionService.submit(callable);
taskExecutor.submit(callable);
while (!future.isDone()) {
// Do some work...
System.out.println("Working on something...");
}
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我有一个工作线程,它位于后台,处理消息.像这样的东西:
class Worker extends Thread {
public volatile Handler handler; // actually private, of course
public void run() {
Looper.prepare();
mHandler = new Handler() { // the Handler hooks up to the current Thread
public boolean handleMessage(Message msg) {
// ...
}
};
Looper.loop();
}
}
Run Code Online (Sandbox Code Playgroud)
从主线程(UI线程,不重要)我想做这样的事情:
Worker worker = new Worker();
worker.start();
worker.handler.sendMessage(...);
Run Code Online (Sandbox Code Playgroud)
麻烦的是,这为我设定了一个漂亮的竞争条件:在worker.handler阅读时,没有办法确定工作线程已经分配给了这个领域!
我不能简单地Handler从Worker构造函数创建,因为构造函数在主线程上运行,因此Handler它将自己与错误的线程相关联.
这似乎不太常见.我可以提出几个解决方法,所有这些都是丑陋的:
像这样的东西:
class Worker extends Thread {
public volatile Handler handler; // actually private, of course …Run Code Online (Sandbox Code Playgroud)首先让我把它扔出去,我知道下面的代码不是线程安全的(更正:可能).我正在努力的是找到一个实现,并且我实际上可以在测试中失败.我现在正在重构一个大型WCF项目,它需要一些(大部分)静态数据缓存并从SQL数据库中填充.它需要每天至少过期和"刷新"一次,这就是我使用MemoryCache的原因.
我知道下面的代码不应该是线程安全的,但我不能让它在繁重的负载下失败,并且使谷歌搜索显示两种方式的实现变得复杂(有和没有锁结合辩论,无论它们是否必要.
在多线程环境中具有MemoryCache知识的人可以让我明确地知道我是否需要在适当的地方锁定,以便在检索/重新填充期间不会抛出删除调用(很少被调用但是它的要求).
public class MemoryCacheService : IMemoryCacheService
{
private const string PunctuationMapCacheKey = "punctuationMaps";
private static readonly ObjectCache Cache;
private readonly IAdoNet _adoNet;
static MemoryCacheService()
{
Cache = MemoryCache.Default;
}
public MemoryCacheService(IAdoNet adoNet)
{
_adoNet = adoNet;
}
public void ClearPunctuationMaps()
{
Cache.Remove(PunctuationMapCacheKey);
}
public IEnumerable GetPunctuationMaps()
{
if (Cache.Contains(PunctuationMapCacheKey))
{
return (IEnumerable) Cache.Get(PunctuationMapCacheKey);
}
var punctuationMaps = GetPunctuationMappings();
if (punctuationMaps == null)
{
throw new ApplicationException("Unable to retrieve punctuation mappings from the database.");
}
if (punctuationMaps.Cast<IPunctuationMapDto>().Any(p => p.UntaggedValue …Run Code Online (Sandbox Code Playgroud) 为什么Thread类被实现为常规类而不是抽象类,run()方法是抽象的.
它可能会引入任何问题吗?或者以这种方式有用吗?
此外,该Thread.start()方法应该是一个非常具体的方法,其功能不能由任何其他类实现(如果我没有错).因此我猜这个final关键字比任何其他方法更适合这个.
但是我可以覆盖这个方法并按我喜欢的方式使用它,
public class Test extends Thread {
public static void main (String... args) {
Thread test = new Test();
test.start();
}
@Override
public void run() {
System.out.println("New thread started...");
}
@Override
public void start() {
System.out.println("Did anyone tell you I will spawn a new thread??");
}
}
Run Code Online (Sandbox Code Playgroud)
它显然只是印刷,
有没有人告诉你我会产生一个新线程?
除了混淆工程师取代你之外,是否有任何超越的用途?
如果没有,为什么这个方法在Thread类中没有声明为final?
multithreading ×10
java ×3
android ×2
c# ×2
.net ×1
bash ×1
concurrency ×1
handler ×1
ios ×1
linux ×1
locking ×1
looper ×1
memorycache ×1
multitasking ×1
python ×1
shell ×1
thread-local ×1
wcf ×1
xcode ×1