Java Learning Notes (12) Multithreaded programming

Source: Internet
Author: User

Concurrent programming: A programmatic way to enable multiple tasks to run concurrently.

Process: A running program that has its own separate piece of memory space, and the internal data and state of each process are completely independent. You can run two or more programs at the same time.

Threads: Each task is called a thread, and a program that runs multithreaded within a program is called a multithreaded program. A thread is a single sequential control flow within a process, and is the smallest processing unit.

To create a thread:

1. Inherit the thread class, rewrite the Run () method, and write the code to execute in the run () method. The thread object is created first, and the start () thread is called by the dot call.

2. Implement the Runable interface, only the run () method. The main () method is the main thread.

Priority: Up to 10, lowest 1, default 5.

Thread synchronization: Startup Object Lock (Synchronized) uses synchronous blocks of code, using synchronous methods.

Deadlock: Two threads are hungry, waiting for the other person to release the object lock, causing each other to be unable to execute down.

Cases:

public class multithreaddemo{

public static void Main (String [] args) {

thread T1 = new Thread (new MyTask (), "Thread A");

T1.start ();

Thread t2 = new Thread (new MyTask (), "thread B");

T2.start ();

System.out.println ();

}

}

Class MyTask implements Runnable {

public void Run () {

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

The I value of System.out.println (Thread.CurrentThread (). Gtname () + "is:" +i);

}

}

}

Java Learning Notes (12) Multithreaded programming

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.