Java concurrency topic details

Source: Internet
Author: User
Tags class generator

Concurrency is a basic topic in computer science. This article will discuss the writing and usage of multithreading in JAVA in four topics. There are four topics: thread definition, shared restricted resources, inter-thread collaboration, and performance tuning. 1 Thread definition: new Thread (new Runnable () {public void run () {/** write it here if you want the Thread to do something. Split the current statement, you can write other statements */}}). start (); 2. Limited shared resources: in other words, the shared resources are locked so that only one thread can access the resources. There are three types of locks: object lock, class lock, and code block lock. 2.1 object lock public class Counter {private Long id = 0L; public synchronized void increase () {id ++;} public synchronized void printCounter () {System. out. println (this. id) ;}} class Counter has two Synchronization Methods: increase () and printCounter (). On the same object, the two methods cannot be executed at the same time and are mutually exclusive. Of course, different objects can. 2.2 class lock public class Generator {private static Long id = 0L; public synchronized static void next () {id ++;} public synchronized static void print () {System. out. println (Thread. currentThread (). getName (); System. out. the println (id) ;}} class Generator has two Synchronization Methods: next () and print (). On the same object, the two methods cannot be executed at the same time and are mutually exclusive. Different objects cannot be executed simultaneously and mutually exclusive. 2.3 code block lock public class CatalogContentSyn extends HttpServlet {protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {synchronized ("lockcata ") {/** here is an operation to read and write the database. when accessing this place, * the previous access must be completed before the access can be executed. If the previous access fails to complete the block code, * the access is blocked. */}}} The previous access must be completed before the access can be executed. If the previous access fails to complete the block code, the access will be blocked. 3. Inter-thread collaboration: public class ListStack {private ArrayList <Integer> list = new ArrayList <Integer> (100); private Boolean hasWait = false;/** stack compression method, if the stack contains data and threads are waiting, wake up other threads */public synchronized void push () {Random rand = new Random (); list. add (rand. nextInt (10000); if (list. size ()> 0 & hasWait) {this. yyall (); hasWait = false ;}}/** method of outbound Stack: the first data in the stack. If no data is found, the thread waits */public synchronized Integer pop () {Integer firstElement = null; if (null! = List & list. size ()> 0) {firstElement = list. get (0); list. remove (0);} else {try {hasWait = true; this. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}} return firstElement ;}in simple words, thread collaboration is a situation where thread 1 waits, and under some conditions, waiting threads are awakened by other threads.

Related Article

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.