Black Horse programmer _java Multithreading single case design

Source: Internet
Author: User

-------<a href= "http://www.itheima.com" target= "blank" >android training </a> <a href= "/http/ Www.itheima.com "target=" blank ">java training </a>, look forward to communicating with you! ----------

I. Threading Methods
    • 1.yield yield CPU
    • 2.setPriority () Sets the priority of the thread
Two. Single Case design mode
  • Singleton design Pattern: guarantees that the class has only one object in memory.

  • How do you ensure that the class has only one object in memory?

    • (1) control the creation of classes, and do not allow other classes to create objects of this class. Private
    • (2) Define an object of this class in this class. Singleton s;
    • (3) to provide public access. public static Singleton getinstance () {return s}
  • There are two types of single-case notation:

    • (1) A Hungry man-type development in this way.
    • //饿汉式class Singleton {    //1,私有构造函数    private Singleton(){}    //2,创建本类对象    private static Singleton s = new Singleton();    //3,对外提供公共的访问方法    public static Singleton getInstance() {        return s;    }    public static void print() {        System.out.println("11111111111");    }}
    • (2) Lazy-type interview to write this way. Multi-threaded problem?
    • //懒汉式,单例的延迟加载模式class Singleton {    //1,私有构造函数    private Singleton(){}    //2,创建本类对象    private static Singleton s;    //3,对外提供公共的访问方法    public static Singleton getInstance() {        if(s == null)            //线程1,线程2            s = new Singleton();        return s;    }    public static void print() {        System.out.println("11111111111");    }}
    • (3) Third form
    • class Singleton {    private Singleton() {}    public static final Singleton s = new Singleton();//final是最终的意思,被final修饰的变量不可以被更改}
    • The runtime class is a singleton class
    • Runtime r = Runtime.getRuntime();//r.exec("shutdown -s -t 300");     //300秒后关机r.exec("shutdown -a");              //取消关机
    • Timer class: Timer

      public class Demo5_Timer {    /**     * @param args     * 计时器     * @throws InterruptedException      */    public static void main(String[] args) throws InterruptedException {        Timer t = new Timer();        t.schedule(new MyTimerTask(), new Date(114,9,15,10,54,20),3000);        while(true) {            System.out.println(new Date());            Thread.sleep(1000);        }    }}class MyTimerTask extends TimerTask {    @Override    public void run() {        System.out.println("起床背英语单词");    }}
Three. Communication between threads
  • 1. When to communicate
    • When multiple threads are executing concurrently, the CPU is randomly switching threads by default
    • If we want them to execute regularly, we can use communication, for example, each thread performs a print
  • 2. How to Communicate
    • Call Wait () if you want the thread to wait
    • If you want to wake up the waiting thread, call notify ();
    • Both methods must be executed in the synchronization code, and the synchronization lock object is used to invoke the
  • 3. Issues with multiple thread communication
    • The Notify () method is a random wake-up of a thread
    • The Notifyall () method is to wake all threads
    • JDK5 Unable to wake a specified thread before
    • If the communication between multiple threads, you need to use Notifyall () to notify all threads, use while to repeatedly judge the condition
Four. After JDK5, the line program control system
    • 1. Synchronization
      • Synchronizing using the Lock () and Unlock () Methods of the Reentrantlock class
    • 2. Communication
      • Use the Newcondition () method of the Reentrantlock class to get the condition object
      • Use condition's await () method when you need to wait, wake up with the signal () method
      • Different threads use different condition so that you can tell which thread to look for when waking up.
Five. Summary of synchronous and non-synchronous classes
    • StringBuffer and Stringbuilder,stringbuffer are thread-safe, inefficient, StringBuilder are thread insecure, high efficiency
    • Vectors and Arraylist,vector are thread-safe, inefficient, ArrayList are thread insecure and highly efficient
    • Hashtable and hashmap,hashtable are thread-safe, inefficient, hashmap are thread insecure, high efficiency

Black Horse programmer _java Multithreading single case design

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.