Search on the Internet a lot, did not search this aspect of the content, their own shallow to write some.
1,. NET and Java's main thread and child threads, the principle is similar
(1), when the child thread is the foreground thread, the main thread ends and the child thread does not end (also continues to run, that is, the process is not finished)
(2), if the child thread into a background thread, the main thread ends, the child thread will also end (thus the process will also be closed)
Examples of. NET code are, for example, Java-like
<summary>///Test main Line Cheng Zi thread end problem///</summary>static void Testthreadparentandson () {// Create a thread through Parameterizedthreadstart string a = "ddd";//Here, when a sub-thread is going to pass "formal parameters", it needs to use Parameterizedthreadstart to power words (no formal parameters, With ThreadStart),//java, multi-threaded, pass formal parameters, also with final decoration//java, multi-threaded, shared variables why in main () must be final modified thread thread = new Thread (new Parameterizedthreadstart (THREAD1));//This proves that when a child thread is a foreground thread, the main thread ends, the child thread is not finished (and will continue to run, that is, the process is not finished)//If the child thread is changed to a background thread, the main thread ends, The child thread will also end (thus the process will also close)
The default is foreground thread//thread. IsBackground = true;//The method to pass the value of thread. Start (a);//This sentence, is the input parameter, can be executed down console.readkey (); Thread th = thread.currentthread;//Gets the currently working threads th. Abort ();} <summary>///creating a method for a parameter///NOTE: The parameter type inside the method must be object type///</summary>///<param name= "obj" ></param >static void Thread1 (object obj) {obj + = "haha"; while (true) {Console.WriteLine (obj+ "" +datetime.now);}}
2. The principle and implementation of multi-threading shared variables in Java and. NET are very different
(1). NET child threads can use the main thread's variables, and the variables change in the child thread, and the main thread changes. If you have more than one child thread, then there will be a question of whether to lock
Static voidMain (string[] args) { intSharedvalue =0; Thread T1=NewThread (() = { //Do something.Sharedvalue + =3; Console.WriteLine ("the child thread value is a:"+sharedvalue); Thread.Sleep ( +); Console.WriteLine ("the child thread value is B:"+sharedvalue); Console.ReadLine (); }); T1. Start (); Sharedvalue+=1; //Thread.Sleep (+);Console.WriteLine ("The main thread ends a, and the shared value is:"+sharedvalue); Thread th= Thread.CurrentThread;//gets the current working threadth. Abort (); Console.WriteLine ("The main thread ends B, and the shared value is:"+sharedvalue); }
(2), for Java, the main thread Cheng line Cheng, or use the main thread in the sub-thread of the variable, then this variable should be added final decoration
public class Testcs {
Verifying multi-threading, parameter-transfer issues
public static void main (string[] args)
{
int c=3;
try {
System.out.println ("Hello World");
Test (c);
} catch (Exception e) {
E.printstacktrace ();
}
File File = new file ("D:\\dev3\\ Unified channel management \\EB. Salechannelplatform ");
Deletefiles (file);
}
/**
*
* @param b
*/
public static void test (final int b) {
Final int a = 10;
At this time the main thread bar with the test method, then this method is still in the main thread, only to the next sentence new thread and start (thread inside the program is a child thread)
New Thread () {
public void Run () {
System.out.println (a);
System.out.println (b);
};
}.start ();
}
}
Java multithreading and. NET Multithreading differences and linkages