Javase Basic Review strategy "nine"

Source: Internet
Author: User

This article summarizes the threading mechanism in Java, and when it comes to threading, you are sure to ask what the difference is between threads and processes. I also had this question when I was in touch with the process, so I'll give you a brief introduction to the process and thread today. process is a computer program on a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure; a thread is a sequential control flow within a program. The difference between them:

Each process has a separate code and data space, the process of switching between the more overhead, the thread can be seen as a lightweight process, with the process of sharing code blocks and data space, each process has a separate running stack and program counter (PC), the switching overhead between threads is small.

Java threads are implemented by the Java.lang.Thread class, and the VM starts with a thread defined by the main method (public static void Main () {}). You can create a new thread by creating an instance of the thread. Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the method run () is called the thread body. Start the thread by invoking the start () method of thread.

1. Thread creation and startup:

 Public classTh {/**     * @paramcreating and starting a thread*/     Public Static voidMain (string[] args) {//The first method of thread creationRunnable Run =Newmyrunnable (); Thread Th1=NewThread (run); //second method of thread creationMyThread Th2 =NewMyThread (); //Thread StartTh1.start ();        Th2.start ();  for(inti=0; i<50; i++) {System.out.println ("Main Thread"); }    }}classMyrunnableImplementsrunnable{ Public voidrun () { for(inti=0; i<50; i++) {System.out.println ("Thread One"); }    }}classMyThreadextendsthread{ Public voidrun () { for(inti=0; i<50; i++) {System.out.println ("Thread Two"); }    }}

2, the basic control of the thread:

  

3. Sleep, join, Yield:

Sleep (int time): Hibernate, Current thread sleeps time milliseconds

Join (): Thread merge

Yield: the opportunity to give out the CPU to other threads

Instance code:

 Public classSJY {/**     * @paramuse of sleep, join, yield*/     Public Static voidMain (string[] args) {Runnable run=NewMyrun (); Thread Th1=NewThread (run); Myth Th2=Newmyth ();  for(inti=0; i<50; i++){            if(i!=25) {System.out.println ("Main Thread"); }Else{Th1.yield ();//Let the CPU out}} th1.start ();        Th2.start (); Try{th1.join ();//Thread MergeTh1.sleep (1000*3);//Sleep 3 seconds}Catch(interruptedexception e) {e.printstacktrace (); }    }}classMyrunImplementsrunnable{ Public voidrun () { for(inti=0; i<50; i++) {System.out.println ("Thread One"); }    }}classMythextendsthread{ Public voidrun () { for(inti=0; i<50; i++) {System.out.println ("Thread Two"); }    }}

4. Thread Priority:

Java provides a thread scheduler to monitor all threads in the program that have entered a ready state after it has been started, and the thread scheduler decides which thread to execute according to the priority of the thread. The priority of a thread is represented by a number, ranging from 1~10, to a thread with a default priority of 5. Thread.min_priority=1; thread.max_priority=10; Thread.norm_priority=5.

Use the following method to get the priority of the thread and set the priority of the thread: int getpriority (): Get thread priority, void setpriority (int newpriority): Set Thread Priority

Temporary some things out, half an hour later for everyone to summarize the mechanism of thread synchronization and locking.

Javase Basic Review strategy "nine"

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.