Black Horse programmer _java08_ Multithreading

Source: Internet
Author: User

Reproduced in: http://www.itxuexiwang.com/plus/view.php?aid=148

A thread is a task that can be executed in parallel in a program.
The Java runtime always executes by selecting the highest-priority thread that is currently in the ready state. If several ready threads have the same priority, the time slice method will be used to take turns to allocate the processor.


The difference between a process and a thread


The process belongs to a separate running program, and the thread is a branch inside the program. Many threads make up the run of a program.


Two ways to create a thread


(1) Thread classes are represented by the thread class and its subclasses, and the inherited run () method defines the task body when the thread executes, defining a thread class that inherits from thread to override the run () method.
(2) Any object that implements the interface runnable can be the target object of a thread, and the class thread itself implements the interface runnable, and the Run () method in the interface needs to implement the task body for the thread execution.
(3) The thread's class method CurrentThread () can be used to obtain information about the current execution threads.


Method One:


public class Person extends thread{
Public person (String name) {
This.name=name;
}
@Override
public void Run () {
Super.run ();
}
}
1
2
3
4
5
6
7
8
9
Method Two:


Class Res
{
private String name;
Private String sex;
Boolean flag=false;
Public synchronized void set (String name,string sex)
{
if (flag)
{
Tr


This article from "It Daniel share" blog, declined reprint!

Black Horse programmer _java08_ Multithreading

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.