Java Multi-Threading and Concurrency Library Advanced Application Study notes lesson 1-9

Source: Internet
Author: User
Tags time in milliseconds

SOURCE xxx, lest advertise suspicion.

Http://www.cnblogs.com/whgw/archive/2011/10/03/2198506.html

Read the article today to find the best way to create a thread for the implementation of the Runnable interface, before the custom to change the LU.

http://blog.csdn.net/imzoer/article/details/8500670

Usage of timer in Java

 Packagetimer;ImportJava.util.Calendar;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classTimertest {StaticTimer timer =NewTimer ();  Public Static voidMain (string[] args) {//task task to is scheduled. //delay delay in milliseconds before task was to be executed. //period time in milliseconds between successive task executions.Timer.schedule (NewMytimertask (), 2000); }}/** Execution interval of 2 seconds alternating with 4 seconds*/classMytimertaskextendsTimerTask {Static BooleanChange =true; Calendar Calendar=calendar.getinstance (); @Override Public voidrun () { change= !Change ;        System.out.println (Calendar.gettime ()); NewTimer (). Schedule (NewMytimertask (), change? 2000:4,000); }}

Thread-Safe is the read, write, and transaction completion stages of multithreading for the same resource object.

Http://www.cnblogs.com/mengdd/archive/2013/02/16/2913806.html

Java Synchronized keyword explanation

Figure out the size of the object synchronized this keyword in different situations, as well as the range.

 PackageThread;/*** sub-thread 2 times, the main thread 4 cycles alternating 10 times. * */ Public classAlternateloop {StaticSysout Sysout =NewSysout ();  Public Static voidMain (string[] args) { for(inti = 0; I < 10; i++) {sysout.subbusiness (i);        Sysout.mainbusiness (i); }    }}//class MyThread implements Runnable {//@Override//Public void Run () {//AlternateLoop.sysout.subBusiness (1);// }// }////class MyThread2 implements Runnable {////@Override//Public void Run () {//AlternateLoop.sysout.mainBusiness (i);// }// }/** Same resource management business logic*/classSysout {Private BooleanIssub =true;  Public synchronized voidMainbusiness (inti) { while(issub) {Try {                 This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); }        }         for(intj = 0; J < 4; J + +) {System.out.println ("Mainbusiness" +j+ "loop of" +i); } issub=true;  This. Notify (); }     Public synchronized voidSubbusiness (inti) { while(!issub) {            Try {                 This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); }        }         for(intj = 0; J < 2; J + +) {System.out.println ("Subbusiness" +j+ "loop of" +i); } issub=false;  This. Notify (); }}

Java concurrency programming: deep anatomy threadlocal

Each thread has a corresponding thread replica object.

 PackageThread;ImportJava.util.Random; Public classThreadlocaltest { Public StaticThreadlocal<integer> ThreadLocal =NewThreadlocal<integer>();  Public Static voidMain (string[] args) { for(inti = 0; I < 2; i++) {            NewThread (NewRunnable () {@Override Public voidrun () {intData=NewRandom (). Nextint (); System.out.println (Thread.CurrentThread (). GetName ()+ "has put data:" +data);                    Threadlocal.set (data); NewA (). get (); NewB (). get ();        }}). Start (); }    }    Static classA { Public voidget () {intData=Threadlocal.get (); System.out.println ("A from Thread" + thread.currentthread (). GetName () + "" +data); }    }        Static classB { Public voidget () {intData=Threadlocal.get (); System.out.println ("B from Thread" + thread.currentthread (). GetName () + "" +data); }    }}

The above example needs to be modified to use this kind of internal static class is not scientific ah.

Java Thread: Atomic (atomic) JDK 1.5 new features

Java Concurrency Programming: the use of thread pools
 PackageThread;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.TimeUnit; Public classThreadpooltest { Public Static voidMain (string[] args) {Executorservice ThreadPool=Executors.newcachedthreadpool ();//executorservice ThreadPool =executors.newfixedthreadpool (ten);//Executorservice ThreadPool = Executors.newsinglethreadexecutor ();         for(inti = 0; I < 10; i++) {            Final intTask = 1; Threadpool.execute (NewRunnable () {@Override Public voidrun () { for(intj = 0; J < 10; J + +) {                        Try{Thread.Sleep (200); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ "is looping of" + j + "for task of" +task);        }                }            }); } executors.newscheduledthreadpool (3). Scheduleatfixedrate (NewRunnable () {@Override Public voidrun () {System.out.println ("Bombing!"); }        }, 6, 2, Timeunit.seconds); }}

Java Multi-Threading and Concurrency Library Advanced Application Study notes lesson 1-9

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.