Home > Back-end >  Thread instance and what is the difference between ordinary class instance?
Thread instance and what is the difference between ordinary class instance?

Time:09-18


 class MyThread extends Thread {
Private int money;
Public void the run () {
System. Out.println (" hello China!" );
}

Void method1 () {

}
}
MyThread mMythread=new MyThread ();




1 thread life cycle such as into the state of death (concept: thread or is abnormal exit, the thread ends, life cycle), why don't mMythread in memory be recycled?

The results of actual test, mMythread. Start (); Finished after the operation, the run (), mMythread this thread into the death of the state of the life cycle, but mMythread this instance in memory still exists, and so I think the thread of the life cycle and its instances in memory does not exist is the concept of different,

2 this Thread instance mMythread and what is the difference between ordinary class instance?

MyThread whether can be considered in addition to run method is very special, its member variables (int money), the ordinary method method1 (); And ordinary classes?


CodePudding user response:

1. Different concepts, relations with threads, thread class instance is a bit like procedures and process, the relationship between process isn't hang up, program is lost,,
2. Like ordinary class

CodePudding user response:

Thread is the essence of the new up a thread stack perform a function, because all Java objects, executing thread have to use such packing, you can see need thread start a function as the entrance, you can also think the run is a Java thread execution entrance,

CodePudding user response:

I think, as a thread Mythread, his life cycle, is the death ended; But the same, he is an instance of a class, as a class, his life cycle, namely recycling, should be treated by the garbage collector, this should be different tasks, just like life, from birth to death, there is no life after death, but the body is still there, and decomposition of entity, hand over to microbial processing

CodePudding user response:

1. The Thread class in the initial state (new) didn't start and end state, if there is no reference will be garbage collection,
2. No distinction,
Conclusion: all classes in Java, Thread to say special, may be only one, is the time when the Thread to run even if no any reference code, he will not be garbage collected (speculated that may have to run inside the virtual machine of threads in the reference), Thread class start later, use the Thread of the operating system resources, the operating system Thread and Thread class completely are two concepts, Thread class only by operating system apis encapsulation using the Thread of the operating system, with the Socket class with operating system, the Socket File class encapsulates the concept of the operating system File,

CodePudding user response:

Thread instances and other instances of the class is no different from just doing different look at the start method to adjust start0 a local method
  • Related