The use of multi-threading in Java!!

Source: Internet
Author: User

Brief introduction:

1. To understand multi-threading, first we need to understand the processes and threads. So what is a process? A process is a running program that allocates memory so that an application can run as a process. So what's the thread again? Threads: In a program, the execution of code is called a thread in a process that performs the path strength. However, in one process, while multiple threads are performing different tasks, we are called multithreading.

2. Why should we use multithreading? Because in a process, sometimes perform tasks, not only to perform a task, it is possible to perform multiple tasks at the same time, we have to use multithreading to solve this problem! such as ticket sales, such as there are three windows, while selling the same type of 50 tickets, window one sold the 50th, then there are 49, in the sale of the 49th ticket, there may be the first window to sell the 49th ticket, it may be the second window to sell the 49th ticket, it may be the third window to sell out, All this time we use multithreading to solve this problem. So in the example above, how do we use code to implement this function?

Code Implementation :

  

Class Saletickets extends thread{//Set total number of votes static int num = 50;        Static object o = new Object ();        Public saletickets (String name) {super (name);                    }//Override the thread's Run method public void run () {////with a loop to simulate the function of ticket selling while (true) {                             Locking synchronized ("lock") {//in order to see the effect better, let it sleep for 0.5 seconds                             try {thread.sleep (500);                             } catch (Interruptedexception e) {e.printstacktrace ();                             } if (num > 0) {System.out.println (This.getname () + "sold the first" +num+ "Ticket");                  num--;                            }else {System.out.println ("The ticket has been sold out");                  Break }}}}}public class Demo2 {Public static void Main (string[] args) {saletickets S1 = new Saletickets ("Window 1");                 S1.start ();//thread saletickets s2 = new saletickets ("Window 2");                S2.start ();//thread saletickets s3 = new Saletickets ("Window 3"); S3.start ();//Thread}}

Summarize:

The use of multithreading can help us to solve a process at the same time to perform a different task, improve the utilization of resources. However, it also increases the burden on the CPU, reduces the execution probability of the threads in the process, and is prone to thread safety problems, which can cause deadlock. All threads are not open as much as possible!

The use of multi-threading in Java!!

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.