J2SE Summary (ii)--Threading

Source: Internet
Author: User

1. The focus of this chapter

Threading concept: The different execution paths contained within a program at the same time , the branch of the program

Create and start

scheduling, priority

State control

Synchronous, asynchronous

2. Process and Thread differences

Process: Static, Exe,class,dos only supports single process

Substance: Execution of threads

3. Create and start

1 ) to achieve runnable Interface

Thread myThread =newthread (target)//difference from the second method

There is only one run () method in runnable that defines the method to be executed by the specific thread

Thread Static Call

Start (): Start thread

Instance:

Class Runner1 implements runnable{

public void Run () {

for (int i=0;i<100;i++) {

System.out.println ("Runner1:" +i);

}

}

}

public class testthread1{

public static void Main (string args[]) {

Runner1 r=new Runner1 (); 1.1

R.run ();//method call instead of starting a new thread

Thread t=new thread?//1.2

T.start ();//Start

for (int i=0;i<100;i++) {

System.out.println ("Main thread ..." +i);

}

}

}

2) directly inherit from thread

Public Runner1 extends thread{

public void Run () {

for (int i=0;i<100;i++)

{

System.out.prinln ("Runner1:" +i);

}

}

}

Since it has been inherited from thread, it is not necessary to newthread in main thread main, only start start will be required.

4. State control

Knowledge Points: 1, Sleep:thread static Method-Exception: Interruptedexception

2, Join method: Thread merge, it should be multi-threaded at the same time, but the join method, wait for another thread to finish, and then return to the current thread to continue execution, as if there is only one thread

3, yield (); Give the CPU, the opportunity to execute the other threads

5. Thread synchronization--lock mechanism

public void Add (string name) {

Synchronized (This) {

Method body

}

}

Shorthand:

publicsynchronizaed void Add (string name) {}

Precautions:

Locked objects are not fully locked, but the locked method cannot be accessed, the method is not locked or can be executed, so if the problem involves changing the size of the value, you need to add a lock on each method.

The above is related to the thread involved in the knowledge, for the soft test, only need to understand the thread and the basic concept of the process, the specific code can not be in-depth understanding. In addition, about the operating system related to other knowledge, you can refer to the introduction of the operating system, which also involves disk reading and writing, file read and write, PV operation, the mechanism (producer and consumer issues)

J2SE Summary (ii)--Threading

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.