简体   繁体   中英

when a method is called , which thread will be run in c# and 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 ? ie: (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()

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 ?

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

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

If this gui thread , why does it give error ?

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?)

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)

Thanks

Edit: 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.

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM