Java Learning Day 23rd-Thread

Source: Internet
Author: User
Tags garbage collection thread class ticket
Overview of ThreadsProcess: A program in a computer that has a specific feature running on a dataset one thread: one unit of the computer process multithreading: a process with multiple threads running at the same time JVM:JVM is multi-threaded, and when we run the JVM, the daemon runs the garbage collection thread to clean up objects that are not referenced.
The difference between a process and a thread: processes are isolated from each other and do not share data data can be shared between threads of the same process
implementation of ThreadsThere are two ways to create a new execution thread one way is to declare a class as a subclass of thread. The subclass should override the thread class's Run method. You can then assign and start an instance of the subclass. Thread Start method is used instead of run
Inheritance thread Class 1. The subclass overrides the Run method in the parent class to keep the thread running code in run, that is, thread body 2. The thread is created at the same time that the subclass object is established 3. To open a thread by calling the Start method
class Mythread extends thread{ Public void Run () {   // Business Logic , Thread Body     } } Public class threaddemo{ Public static void Main (string[] args) { new Mythread (). Start (); new Mythread (). Start ();   } }
Another method is to declare a class that implements the Runnable interface. The class then implements the Run method and then assigns an instance of the class, which is passed and started as a parameter when the thread is created. Implement Runnable interface 1. The custom class implements the Run method in the interface. 2. Create a thread through the thread class and pass the subclass object that implements the Runnable interface as a parameter to the thread class constructor 3.Thread class object invoke the Start method to open the thread class Mythread implements runnable{ Public void Run () {   // Business Logic , Thread Body   } } Pulbic class threaddemo{ Public static void Main (string[] args) { mythread t1 = new Mythread (); new Thread (T1). Start (); new Thread (T1). Start ();   } }
Threads Basic Common operationsGet the thread Name: GetName () gets the thread name of the Main method: Thread.CurrentThread (). GetName () Set the name of the threads: Object. SetName () Set Priority: Object. setpriority () Join of Thread: Object. Join ()//after execution, then perform other process threads of humility: Thread.yield () thread control: Object. Setdaemon (Boolean on) thread termination: Object. Stop ()//Obsolete object. Interrupt ()//push Recommended Use

Thread Execution PrincipleThe concurrent execution of threads is the constant switching of CPU resources through multiple threads, which is very fast and we do not feel that we can perceive multiple threads executing concurrently.
life cycle of threadsNEW: Thread is new to be ready: the thread has the qualification to execute, that is, the thread invokes start () and does not execute the right to run: have the qualification to execute and have the right to execute blocking: No execution qualification and execution rights to destroy: Thread object becomes garbage, releasing resources
ConcurrentThere are a number of concurrent cases in the Internet project, such as selling train tickets, and the Electronic Business website. Example: Train to find 100 tickets, 4 windows at the same time to buy tickets analysis: 4 windows are 4 threads running at the same time, 100 tickets are 4 threads of shared resources implemented by inheritance thread, for thread security issues, we need to use synchronization (that is, to lock, share resources Intelligent one person access) lock. Syntax: synchronized (lock object) {//Operations shared resource Code} where is the sync code added? 1. Code is accessed by multiple threads 2. There is shared data in the Code 3. Shared data is manipulated by multiple statements Packagecom.czz.test01; Public classSaleticket extendsthread{ PrivateString name; PublicSaleticket (String name) { Super(name); //plus static, indicating that 100 tickets are shared data Private Static intTickets = 100; Synchronizing Lock objects Private StaticObject obj = NewObject (); @Override Public voidRun () { while( true) {//Sync code block synchronized(obj) { if(Tickets > 0) {System. out. println ( This. GetName () + "is selling the first" + Tickets-+ "ticket"); } Else{System. out. println ("The ticket has been sold out"); Break; Test object with}}}}} Packagecom.czz.test01; Public classtickettest { Public Static voidMain (string[] args) {Saleticket st1 = NewSaleticket ("Window 1"); Saleticket St2 = NewSaleticket ("Window 2"); Saleticket ST3 = NewSaleticket ("Window 3"); Saleticket St4 = NewSaleticket ("Window 4");       St1. Start ();       St2. Start ();       St3. Start ();    St4. Start (); } }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.