如何通过 ThreadSafty 在线程内使用线程

发布于 2024-11-18 16:19:25 字数 720 浏览 4 评论 0原文

有没有办法可以在 Thread 中使用 Thread ?如果是这样,如何以线程安全方式使用它

我在线程内尝试了这个线程,有时我得到了错误的结果。请帮助我如何使用线程安全找出线程内部的线程

for(int i=0; i<numExpression; i++)
{
    final int _i = i;
    final SDISQueryInfo _qryInfo = sdisQueryInfo;
    final SDISQueryComponent _qryComponent = qryCompoents;
    TCreate[i] = new Thread(new Runnable()
    {
        public void run()
        {
            _qryComponent.prepare(_qryInfo);
        }
    });
    TCreate[i].setName(_qryInfo.getQueryTerm(_i));
    TCreate[i].start();
}
for(Thread t : TCreate)
{
    t.join();
}

这里_qryCompoents.prepare(_sdisQueryInfo)是我正在调用的函数。这个函数里面有线程。


很抱歉这个问题我道歉..我无法删除这个问题。

Is there a way where i can i use Thread inside Thread ? if so how to use that in Thread Safety Manner

I tried this Thread inside Thread where i got wrong result sometime. Please Help me how to figure out Thread inside Thread using Thread Safety

for(int i=0; i<numExpression; i++)
{
    final int _i = i;
    final SDISQueryInfo _qryInfo = sdisQueryInfo;
    final SDISQueryComponent _qryComponent = qryCompoents;
    TCreate[i] = new Thread(new Runnable()
    {
        public void run()
        {
            _qryComponent.prepare(_qryInfo);
        }
    });
    TCreate[i].setName(_qryInfo.getQueryTerm(_i));
    TCreate[i].start();
}
for(Thread t : TCreate)
{
    t.join();
}

Here in this _qryCompoents.prepare(_sdisQueryInfo) is function which i am calling. This function has thread inside in it.


Sorry for this question i apology .. i couldn't able to remove this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2024-11-25 16:19:25

原则上从其他线程启动线程没有问题。线程之间没有真正的区别(可以这么说,没有父线程或子线程)。

我不确定你所说的“这个函数里面有一个线程......”是什么意思。

我假设你的意思是它将启动另一个线程。这绝不会有什么大的区别,除非内部线程正在与其他线程共享信息。您必须查阅 SDISQueryComponent.prepare 的文档来了解情况是否如此。

我的期望是 SDISQueryComponent 可以使用线程,原因很简单,它显然是在设计时考虑到线程的(否则它不会启动线程)。

我在这里想到的最重要的事情是,您可能会遇到这样的情况:创建更多线程会产生争用(由于上下文切换开销以及可能的锁定),从而使速度变慢。

在这种情况下,通常的解决方案涉及线程池和工作队列。但是,我不知道 SDISQueryComponent 是否支持使用线程池,因此这可能超出您的控制范围

In principle there is no issue with starting threads from other threads. There is no real difference between the threads (there aren't parent threads or child threads, so to speak).

I'm not sure what you mean by "this function has a Thread inside it...".

I'm assuming you mean that it will start another thread. By no means does that make a big difference, unless of course that inner thread is sharing information with other threads. You'd have to consult the documentation for SDISQueryComponent.prepare to find out whether that is the case.

My expectation is that SDISQueryComponent is ok with threading, for the simple reason that it is obviously designed with threading in mind (or it wouldn't start threads).

The most important thing I'd think of here, is that you might run into the point where creating more threads creates contention (due to context switching overhead and perhaps locking) making things slow.

In that case, the usual solution involves a thread pool and a worker queue. However, I don't know whether SDISQueryComponent supports using a thread pool, so that might be out of your hands

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文