Traditional Threading Technology Review

Source: Internet
Author: User

/**

*

* Description: Traditional Threading Technology Review.

* <p>

*

* Multithreading mechanism will improve the efficiency of the program running?

*

==============================================

No, it will be slower because CPU resources are limited

Why do you have multi-threaded downloads?

is to snatch server bandwidth

==============================================

Not necessarily, multithreading can not improve the CPU frequency, that is, the number of instructions per unit time can be executed, if it is a single-threaded task,

CPU can only deal with single-threaded, the multi-threaded to deal with, may reduce the efficiency of the operation, such as a person, three tables,

Make a steamed bun at a table, and then go to another table to do it, or just do it on a table fast?

Obviously it's faster to do it on a table, because it takes time to run to the other table, which is the CPU switching thread.

Why use multithreading?

1. Points of concern, separation

2. Improve performance

Example:

There are many factors that affect efficiency, such as waiting for a resource monopoly, a more time-consuming file upload, a file download, or a long chat with a customer.

Establish a connection, if a thread to deal with it is not service too many customers.

Multithreading will improve performance, do more things at the same time, serve more customers

*

* </p>

*/

public class Traditionalthread {/** * @param args */public static void main (string[] args) {/** * written in 1 Th Read thread = new Thread () {//Overwrite the Run method in the thread class @Overridepublic void Run () {while (true) {try {Thread.Sleep (500) ;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("The current thread object name gets Method 1:" + thread.currentthread (). GetName ());//this represents the current object thread, which is not recommended. It is now very clear who this is System.out.println ("The current thread object name gets Method 2:" + this.getname ());}};     Thread.Start (); *//** * Writing Method 2: Incoming public thread (Runnable target) * New Runnable () {The host that the thread is running * This notation is more of an object-oriented programming Thread thread2 = new Thread (new Runnable () {@Overridepublic void run () {while (true) {try {thread.sleep ()} catch (Int Erruptedexception e) {e.printstacktrace ();} System.out.println ("1:" + thread.currentthread (). GetName ());//This does not represent the current object//system.out.println ("The current thread object name gets Method 2: "+ this.getname ());}}); Thread2.start (); */new Thread (New Runnable () {public void run () {while (true) {try {thread.sleep ()} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("runnable:" + thread.currentthread (). GetName ());}}) {public void Run () {while (true) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread:" + thread.currentthread (). GetName ());}}. Start ();}

  



Traditional Threading Technology Review

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.