CGBTN2111-DAY17 summary review

1. Multi-threaded data security hidden danger solution

1. Reasons for data security issues:
  1. multithreaded program
  2. Multiple threads own shared data
  3. Multiple statements operate on shared data
2. synchronous and asynchronous

Asynchronous: It is the effect of multiple threads preempting resources, not queuing, so the efficiency is high, but the data is not safe Synchronization
: Only one thread monopolizes resources at a time, queuing, so the efficiency is low, but it is safe, and some resources should be sacrificed for safety.
Synchronization Keyword

3. Solution: locksynchronized

Synchronized code block[Common use], format:
synchronized (unique lock object) { all codes that may have data security issues }

4. Precautions when using synchronization:
  1. The lock object must be unique! ! !
    For example: if it is the way of implementing the interface, only one target business class object (interface implementation class object) is created, then there is only one lock object For example 2: if it is the way of
    inheriting the Thread class, you may need to create multiple subclass objects , then you need to add static to the lock object at this time to ensure that the lock object is uniquely shared by all objects
    . Thread implementation is used more
  2. There is no limit to the type of lock object, as long as it can be guaranteed to be unique
  3. The scope of locking needs to be carefully considered.
    It should not be too large or too small. Too large will waste efficiency, and too small will not lock.
5. The solution to the problem in the case of multi-threaded ticket sales:

1. Create 4 thread objects and sell 400 tickets:
Solution: Set the number of tickets to be static and shared by all objects globally
2. The number of tickets is resold (one ticket is sold to multiple people):
Solution: Use synchronous code blocks to ensure that only one thread sells tickets at a time.
3. The number of tickets is oversold (tickets sold out of range 0 -1 -2): Solution:
optimize the code logic, and sell tickets when there are tickets , Stop when there is no ticket, there are many options, the test results shall prevail.
For detailed code and notes, please refer to:

Click here for detailed notes on multi-threaded ticketing case and synchronous lock solution

2 How threads are created 3

Multi-threaded programming implementation scheme three: the way Executors create thread pools

  1. A tool class for creating a thread pool:
    Executors.newFixedThreadPool(int n); can create a thread pool object containing up to n threads
  2. Created thread pool object: ExecutorService
    uses pool.excute() to start threads in the thread pool in a multi-threaded manner. Each call will add a thread object to the ready queue.
    This thread pool object is responsible for: New/ Start/shut down threads, and we are mainly responsible for custom business.
    Note: the thread pool is not closed, and the effect is that the thread objects in the thread pool can be used at any time, thus avoiding frequent creation and destruction of threads. no waste of resources
  3. Reasonable use of the thread pool can have advantages:
    1. Reduce system resource consumption: reduce the number of times the system creates and destroys thread objects, and each thread can be reused to perform multiple tasks.
    2. Improve response speed: when a task arrives, Tasks can be executed immediately without waiting for thread creation
    3. Improve the manageability of threads: the number of threads in the thread pool can be adjusted according to the capacity of the system
    to prevent the server from crashing due to excessive memory consumption caused by creating multiple threads
    [per Each thread needs about 1MB of memory. The more threads are opened, the more memory will be consumed, and finally crash]

3 design patterns

  1. Concept: It is a programming "routine" worth learning summed up by some predecessors. There are 23 design patterns in total.
  2. Singleton design pattern: ensure that there is only one instance of this class in the code
  3. Implementation ideas:
    Option 1: Hungry Chinese style
    1) Make the constructor of this class private – in order to prevent the outside world from calling the constructor to create objects
    2) Create objects through the constructor of this class and privatize this object, in order to prevent outside calls
    3) Provide public global The access point returns the only object of this class to the outside world.
    Note: The public method needs to be set to 静态- skip the object, and directly call the public method that returns the object of this class through the class name.
    The object also needs to be set to 静态- this object needs to be static The method is returned, and the static can only call the static

4. Notes

1. Annotations that come with JDK (5)
		要求大家掌握的是@Override注解,这个注解可以加在方法上,用来表示这是一个重写的方法
2. 5 meta annotations
      元注解是用来定义其他注解的注解,也就是说,注解的语法与JAVA不同,是靠注解来定义的
      1. 定义注解的格式:@interface 注解名
      2. 可以根据元注解对注解进行设置:
      要求大家掌握的是
      表示被描述的注解可以使用的位置:值可以多选

@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
Indicates the statement cycle of the annotation being described: Note that the value can only be 1 out of 3
@Retention(RentionPolicy.RUNTIME/SOURCE/CLASS)

3. Custom annotations
           1. 我们也可以根据自己的需求来定义个性化的注解
               使用的是@interface 注解名来定义的,主要使用的就是上面的两个元注解
           2. 除此之外,我们还可以给注解加功能,比如注解的属性:
               格式:属性类型 属性名(); 比如:int age();
               注意:定义了注解的普通属性以后,使用注解时必须给属性赋值,格式:@Rice(age=10)
                         如果给属性设置了默认值,那么使用注解时就不需要给属性赋值了,格式:int age() default 0;
           3.我们还可以给注解添加特殊的属性value,注意这个属性名字必须是value,类型不作限制
              注意:特殊属性如果不设置默认值,使用注解时也需要赋值,不过赋值可以简写,比如@Rice("apple")
                        特殊属性赋予默认值后,就可以直接使用注解了,赋予默认值的格式:String value() default "apple";
             注意:如果有多个属性,并且都没有赋予默认值,那么使用注解时的格式:@Rice(value="apple",age=10)

Guess you like

Origin blog.csdn.net/weixin_43884234/article/details/122090585