accp8.0 Conversion Textbook 1th Chapter Multithreading Comprehension and Practice

Source: Internet
Author: User
Tags stub thread class

A. Word part:

The ①process process ②current the current ③thread thread ④runnable can be obtained

⑤interrupt Interrupt ⑥join Join ⑦yield generate ⑧synchronize simultaneous occurrence

Two. Preview section

1. Thread-to-process differences:

Process is the basic unit of system running program

A thread is the smallest unit in a process that performs operations

2. What are two ways to create a thread

① inheriting the thread class

② Implementing the Runnable interface

3. The life cycle of a thread can be divided into several stages, and at what stage

Five stages: ① create ② Ready ③ Run ④ block ⑤ death

4. What methods of using threads can set the thread's hibernation, thread enforcement, thread comity

are: Sleep (), join (), yield ()

5. When thread synchronization is required, there are several ways in which thread synchronization occurs

When an access violation is required

Two ways: ① synchronous method ② Synchronous code block

Three. Practice Section

1. Create a thread with a method that inherits the thread class and display the corresponding content

First create a thread class:

Package oneone;

public class Myrunnableone extends thread{

public void Run () {
for (int i=1;i<=20;i++) {
System.out.println (i+ ". Hello, from Thread" +thread.currentthread (). GetName ());

}
}
}

Then create the Main method class and remove the use.

Package oneone;

public class Testone {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Myrunnableone my=new Myrunnableone ();
Myrunnableone my1=new Myrunnableone ();
My.start ();
My1.start ();
}

}

2. Create a thread using the implement Runnable interface method

First create an implementation class:

Package onetwo;

public class Myrunnabletwo implements runnable{

public void Run () {
for (int i=1;i<=20;i++) {
System.out.println (i+ ". Hello, from Thread" +thread.currentthread (). GetName ());

}
}


}

Again the Main method:

Package onetwo;

public class Testtwo {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Myrunnabletwo my=new myrunnabletwo ();
Myrunnabletwo my1=new myrunnabletwo ();
Thread tr=new thread (my);
Thread tr1=new thread (MY1);
Tr.start ();
Tr1.start ();
}

}

3. Multi-person trekking climbing with multi-threaded simulation

First create an inheritance or implementation class (I use inheritance here):

Package onethree;

public class Myrunnablethree extends thread{
private int time;
public int num=0;
Public Myrunnablethree (String name,int time,int kio) {
Super (name);
This.time=time;
this.num=kio*1000/100;

}
public void Run () {

while (num>0) {
try {
Thread.Sleep (This.time);
} catch (Interruptedexception e) {
Todo:handle exception
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + "Climb 100 meters!" ");

num--;
}
System.out.println (Thread.CurrentThread (). GetName () + "Get to the end!" ");
}

}

Again the Main method:

Package onethree;

public class Testthree {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Myrunnablethree young=new myrunnablethree ("Young Man", 500, 1);
Myrunnablethree old=new myrunnablethree ("seniors", 1500, 1);
Myrunnablethree child=new Myrunnablethree ("Child", 600, 1);
System.out.println ("********** starts climbing *********");
Old.start ();
Young.start ();
Child.start ();
}

}

4. Display, set thread priority

To inherit or implement a class first:

Package onefour;

public class Myrunnablefour extends thread{
public void Run () {
Thread.CurrentThread (). setpriority (1);
System.out.println ("sub-thread Name:" +thread.currentthread (). GetName () + ", Priority:" +thread.currentthread (). getpriority ());
}
}

Again main:

Package onefour;

public class Testfour {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Myrunnablefour myf=new Myrunnablefour ();
Myf.start ();
System.out.println ("************* Show Default Priority ********");
System.out.println ("Main thread Name:" +thread.currentthread (). GetName () + ", Priority:" +thread.currentthread (). getpriority ());
Thread.CurrentThread (). setpriority (10);
System.out.println ("*********** changes the default priority after ***********");
Myf.setpriority (1);
System.out.println ("Main thread Name:" +thread.currentthread (). GetName () + ", Priority:" +thread.currentthread (). getpriority ());
System.out.println ("sub-thread Name:" +myrunnablefour.currentthread (). GetName () + ", Priority:" +myrunnablefour.currentthread (). GetPriority ());


}

}

5. Sim Doctor

To inherit or implement a class first:

Package onefive;

public class Myrunnablefive extends thread{
private int time;
public int pertail=0;
Public myrunnablefive (String common,int time) {
Super (common);
This.time=time;

}
public void Run () {
Thread.CurrentThread (). SetPriority (8);
for (int i=1;i<=10;i++) {
try {
Thread.Sleep (This.time);
} catch (Interruptedexception e) {
Todo:handle exception
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Special number:" +i+ "patient in the doctor! ");

}
}

}

Again main:

Package onefive;

public class Testfive {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Myrunnablefive pertail=new myrunnablefive ("special number", 1000);

Thread Temp=new Thread (new myrunnablefive ("special number", 400));
Temp.start ();
Thread.CurrentThread (). setpriority (4);
for (int i=1;i<=50;i++) {

if (i==11) {

try {
Temp.join ();
} catch (Interruptedexception e) {
Todo:handle exception
E.printstacktrace ();
}
}
try {
Thread.Sleep (200);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Ordinary number:" +i+ "patient in the Doctor");

}

}
}

6. Multi-threaded simulation relay race

Create an inheritance or implementation class first:

Package onesix;

public class Runsix implements runnable{
private int meters=1000;
Public Runsix () {


}
@Override
public void Run () {
TODO auto-generated Method Stub
System.out.println ("Come in");
while (true) {
Type type = (type) true.nextelement ();
Synchronized (this) {
if (meters<=100) {
Break

}
System.out.println (Thread.CurrentThread (). GetName () + "Got the baton!" ");
for (int i = 0; i <; i+=10) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + "ran" + (i+10) + "M! ");
}
meters-=100;
}
}
}

}

Again the main interface class:

Package onesix;

public class Testsix {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Runsix ru=new runsix ();
for (int i = 0; i < 5; i++) {
New Thread (Ru, (i+1) + "number Player"). Start ();
}
}

}

7. Multi-threaded analog network ticketing

Peach run, ticket, scalpers, together Rob 10 tickets, limit scalpers can only Rob once ticket

Create an inheritance or implementation class first:

Package Oneseven;

public class Siteseven implements runnable{
private int count=10;
private int num=0;
Private Boolean flag=false;

@Override
public void Run () {
TODO auto-generated Method Stub
System.out.println ("Come in");
while (!flag) {

Synchronized (this) {
System.out.println ("Come in");
if (count<=0) {
Flag=true;
Return
}
num++;
count--;
try {
Thread.Sleep (500);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
String Name=thread.currentthread (). GetName ();
if (Name.equals ("scalpers")) {
System.out.println (name+ "Grab the first" +num+ "ticket, the remaining" +count+ "Ticket! ");
Break
}
System.out.println (name+ "Grab the first" +num+ "ticket, the remaining" +count+ "Ticket! ");
}
}
}

}

Then create the main interface class:

Package Oneseven;

public class Testseven {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Siteseven si=new Siteseven ();
Thread per1=new thread (SI, "dadong");
Thread yellow=new thread (SI, "scalpers");
Thread per2=new thread (SI, "Kai chun");
Per1.start ();
Yellow.start ();
Per2.start ();
}

}

Four: summary:

Methods in the 1.Thread class implement operations on thread objects

① to adjust the priority of a thread

② thread Sleeping Sleep ()

③ the forced run of a thread join ()

④ thread comity yield ()

2. Multithreading allows programmers to write efficient programs that maximize CPU utilization

3. Two ways to create a thread:

① declares a subclass that inherits the thread class and implements the run () method of the Thread class

② declares a class that implements the Runnable interface, and then implements the run () method

accp8.0 Conversion Textbook 1th Chapter Multithreading Comprehension and Practice

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.