A good memory is better than a bad pen. 74-multithreading-multithreading for runnable interfaces

Source: Internet
Author: User

We can implement multithreading by inheriting class thread or implementing the Runnable interface. If you have a class that inherits a class and wants to implement multi-threading, you can implement the Runnable interface.
The run () function must be overwrite and put the code that will be processed in parallel in multiple threads into this function.
A thread object is generated as a parameter to an object that implements the Runnable interface, and then the start () function of the thread object is called to perform parallel operations.
For example:
Easyrunnablethread thread2 = new Easyrunnablethread ();
New Thread (Thread2). Start ();
If the developer calls the run () method directly, it will be executed as a normal serial program, rather than multithreaded.

Java source code for the implementation of simple multithreading

 PackageCom.thread;/** * A simple multi-threaded implementation of the Runnable interface * * @author Fan Fangming */ Public  class easyrunnablethread implements Runnable {    inti =0, j =0, k=0;intFlag =0; Public void Run() {//Let the system run 6 times, in order to observe the results can be         for(intCount =0; Count <6; Count + +) {Try{Thread.Sleep ( -); }Catch(Exception e)            {E.printstacktrace (); }if(Flag = =0) {i++; System.out.println ("i="+ i); }Else if(Flag = =1) {j + +; System.out.println ("b Split point: j="+ j); }Else{k++; System.out.println ("C Split point: k="+ k); }        }    } Public Static void Main(string[] args) {Easyrunnablethread Thread1 =NewEasyrunnablethread (); Thread1.flag =0;//Do not call the Run method directly, otherwise it will be treated as a normal serial thread        //thread1.run ();        NewThread (Thread1). Start (); Easyrunnablethread thread2 =NewEasyrunnablethread (); Thread2.flag =1;NewThread (Thread2). Start (); Easyrunnablethread thread3 =NewEasyrunnablethread (); Thread3.flag =2;NewThread (THREAD3). Start (); }}

Run Results
C Split point: k=1
B Split point: J=1
I=1
B Split point: j=2
i=2
C Split point: k=2
B Split point: j=3
C Split point: k=3
I=3
B Split point: j=4
I=4
C Split point: k=4
B Split point: j=5
I=5
C Split point: k=5
I=6
C Split point: k=6
B Split point: j=6

Similarly, if we call the Run method directly in class, it will also be called as a normal serial method, rather than a multi-threaded operation.

A good memory is better than a bad pen. 74-multithreading-multithreading for runnable interfaces

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.