Java multi-thread runnable

Source: Internet
Author: User

(1)

Steps:

1. Define the runnable interface

2. overwrite the run method in the runnable interface and store the code to be run by the thread in the run method.

3. Create a thread object through the Thread class.

4. Pass the subclass object of the runnable interface as an actual parameter to the constructor of the thread class.

Why do we need to explain the constructor that the subclass object of the runnable interface passes to the thread. Because the object of the custom method is a subclass object of the runnable interface.

5. Call the start method of the thread class to enable the thread and call the runnable interface subclass run method.

(2) thread-safe shared code block

Objective: Is there a security problem in the program? If so, how can this problem be solved?

How to find the problem:

1. Identify the code that is run in multiple threads.

2. clearly share data

3. Determine which statements in multi-threaded code are used to operate on shared data.

class Bank{private int sum;public void add(int n){     sum+=n; System.out.println("sum="+sum);}} class Cus implements Runnable{   private Bank b=new Bank();   public void run(){     synchronized(b){    for(int x=0;x<3;x++) {b.add(100);  } }   }}public class BankDemo{    public static void main(String []args){    Cus c=new Cus();Thread t1=new Thread(c);Thread t2=new Thread(c);t1.start();t2.start();}}
Alternatively, place the Synchronous Code synchronized in the modifier.
class Bank{private int sum;public synchronized void add(int n){    Object obj=new Object();           sum+=n; try{      Thread.sleep(10); }catch(Exception e){   e.printStackTrace(); } System.out.println("sum="+sum);    }} class Cus implements Runnable{   private Bank b=new Bank();   public void run(){        for(int x=0;x<3;x++) {b.add(100);  }    }}public class BankDemo{    public static void main(String []args){    Cus c=new Cus();Thread t1=new Thread(c);Thread t2=new Thread(c);t1.start();t2.start();}}

Summary:

1. Define the problem and method to be handled in a class.

2. Rewrite the run method in the runnable class to call the method to solve the problem in the defined class.

In the synchronized block, accept the object of the class to handle the problem.

3. Define multiple threads for execution in the main method.

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.