当一个方法被调用时,哪个线程将在c#和java中运行? [英] when a method is called , which thread will be run in c# and java?

查看:112
本文介绍了当一个方法被调用时,哪个线程将在c#和java中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都知道我们正在使用多线程平台,并且我们正在开发多线程应用程序.顺便说一句,我无法识别线程问题.当我调用静态方法(在另一个类中)时,将运行哪个线程? gui线程会执行作业并运行方法吗? IE: (我将提供vb代码示例.逻辑与Java相同.)

Everybody knows we are using multi threading platforms and we are developing multi threading applications. By the way, i couldn't recognize the thread issue. When i call a static method (that is in another class.) , which thread will be run ? does gui thread go the jobs and run method ? i.e: (i am gonna give vb code sample . Logic is the same with java.)

Private Sub runValuesTest_Click(sender As System.Object, e As System.EventArgs) Handles RunValuesTest.Click
   DummyClass.Instance.DoJob()
End sub()

这是vb语法,但没有关系.主要逻辑与C#和Java相同.当我单击按钮时,将运行新线程吗?还是gui线程上班?

This is vb syntax but it does not matter . the main logic is the same with C# and Java. When i click the button, will be new thread run ? Or gui thread go job ?

如果将运行新线程,为什么以及何时在工作中使用新线程"?

If new thread will be run, why and when do we use New Thread to our jobs ?

如果gui线程运行了,那么我看到的很多时间都超过了在程序中运行一个线程的时间.程序中的线程如何不同?为什么以及何时以某种方式发生线程异常?例如:我正在使用此方法进行文件操作:(这可能是另一个问题)

If gui thread goes, I see lots of time more than one thread run in a program. how can threads be different in a program? why and when thread exceptions occurs somehow ? For example: i am doing file operation in this method: (this may be different question)

Private _file As System.IO.StreamWriter
Private Sub runValuesTest_Click(sender As System.Object, e As System.EventArgs) Handles RunValuesTest.Click
    If Not Directory.Exists("GraphXml") Then
        Directory.CreateDirectory("GraphXml")
    End If

    _fileName = "GraphXml\Graph_" & txtName & ".xml"

    _file = My.Computer.FileSystem.OpenTextFileWriter(_fileName, False)

    _file.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")

    _file.WriteLine("<n0>")

    DepthFirstSearch(StaticService.AllNodes(0))

    _file.WriteLine("</n0>")
    _file.Close()
    _file.Dispose()
 End Sub

当我单击速度如此之快时(每2秒单击1次),它会显示错误另一个进程正在使用文件".

When i click so fast (1 click per two seconds) , it gives error "Another process uses file".

如果此gui线程为何会出现错误?

If this gui thread , why does it give error ?

如果是new thread,为什么我们要在按钮click方法中做一些类似gui操作的工作人员. ? (每个人都知道,要做gui操作,我们想使用gui线程,对吗?)

If it is new thread , why do we do some staffs like gui operations in button click method. ? (Everybody knows , to do gui operations , we want to use gui thread, am i right?)

这第二个代码部分可能是另一个问题,但我只想学习.net的THREAD背后的逻辑(我认为该逻​​辑对Java也有效)

This second code part may be different question but i just want to learn logic behind of the THREAD for .net ( I think the logic is also valid for java)

谢谢

这可能与JAVA无关.尽管我提供了vb代码示例,但我添加了java标签,因为我认为逻辑是相同的.

this may not be relevant with JAVA. Although i give vb code sample, i added the java tag because i think the logic is the same.

推荐答案

当我调用静态方法(在另一个类中)时,将运行哪个线程?

When i call a static method (that is in another class.) , which thread will be run ?

不要调用方法.您编写代码. 线程调用方法.线程是执行代码的代理.每个程序至少有一个线程.当线程调用某个方法(是否为静态)时,该方法将在调用该方法的线程中被调用.

You don't call methods. You write code. Threads call methods. A thread is an agent that executes your code. Every program has at least one thread. When a thread calls a method (static or not), the method is called in the thread that called it.

当我单击按钮时,将运行新线程吗?还是gui线程上班?

When i click the button, will be new thread run ? Or gui thread go job ?

这是关于Visual Basic的问题.我不了解Visual Basic,但是许多GUI框架都有一个事件处理程序线程".当您单击按钮时,事件将由在事件处理程序线程中运行的框架代码读取.该框架先确定哪个窗口,哪个小部件等,然后如果您的代码已注册处理程序,则事件处理程序线程将调用您的处理程序方法.

That is a question about Visual Basic. I don't know Visual Basic, but many GUI frameworks have an "Event Handler Thread". When you click a button, the event is read by framework code running in the event handler thread. The framework figures out which window, which widget, etc., and then if your code has registered a handler, the event handler thread calls your handler method.

执行gui操作,我们要使用gui线程,对吗?)

to do gui operations , we want to use gui thread, am i right?)

许多GUI框架都是单线程的,这是有力的原因. https://weblogs.java.net/blog/kgh/archive /2004/10/multithreaded_t.html 但是,如果您要问有关Visual Basic的问题,那我将是一无所知.

There is a powerful reason why many GUI frameworks are single threaded. https://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html But again, if you are asking about Visual Basic, that's something I wouldn't know about.

程序中的线程如何不同?

how can threads be different in a program?

为什么以及何时以某种方式发生线程异常?

why and when thread exceptions occurs somehow ?

...?

您可能需要先学习更多英语,然后再尝试用英语提问,或者您需要先了解有关线程的更多知识(通过一些教程来学习),然后再尝试问有关线程的问题.您的很多问题似乎都没有多大意义.

You either need to learn more English before you try asking questions in English, or you need to learn more about threads (work through some tutorials) before you try asking questions about threads. A lot of your questions don't seem to make much sense.

这篇关于当一个方法被调用时,哪个线程将在c#和java中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆