Hello, welcome toPeanut Shell Foreign Trade Network B2B Free Information Publishing Platform!
18951535724
  • Java basic knowledge point - java basic knowledge point for basic knowledge compilation (detailed)

       2026-02-26 NetworkingName1200
    Key Point:Note that when a catch statement is intended to capture a variety of abnormal types, the parameter (in this case, e) implicitly becomes a nonal variable。The way to declare the abnormals, throw the abnormals。Try-with-resources clause (java se 7 later)To quote from the official website:A resource is an object that must be closed after the program is finished with it.ThreadThread is the smallest element of the operating system movement

    Note that when a catch statement is intended to capture a variety of abnormal types, the parameter (in this case, e) implicitly becomes a nonal variable。

    The way to declare the abnormals, throw the abnormals。

    Try-with-resources clause (java se 7 later)

    To quote from the official website:

    A resource is an object that must be closed after the program is finished with it.

    Thread

    Thread is the smallest element of the operating system movement, also known as the lightweight process. The same process creates multiple threads, each with its own calculators, stacks and local variables and has access to shared memory variables。

    Five states of the thread

    Create (new): create a thread using a new keyword

    Ready: call the start method, wait for cpu movement

    Running: execute runm method

    Java knowledge-sharing

    Blocked: suspension of cpu use for a number of reasons

    Death (dead): an anomaly occurs when the method is executed or executed

    A few important terms

    Sync and step

    The synchronous approach must wait for the method to be returned before the subsequent act can be carried out; the staggered approach can be followed immediately。

    Parallel and parallel

    Parallelity is a genuine multiplicity of tasks carried out simultaneously; conjunctively it supports the handling of multiple tasks, not necessarily at the same time, and multiple missions may be collusive, but each mission can only take a short time of cpu, with multiple tasks being carried out interchangeably over a very short period of time。

    I believe you have been able to conclude that the concept of “parallel” is a subset of the concept of “mixed”. In other words, you can write a co-production program with multiple threads or processes, but without a multi-nuclear processor to implement the program, the code cannot be run in parallel. Therefore, programming models or execution actions involving multiple implementation processes in the search for individual issues fall within the scope of simultaneous programming。

    Blockage and non-blocking

    Blockage means that when a thread visits a public resource, the other thread must wait for its release to be used, otherwise it must be hung up; and non-blockage means that there will be no competition between the threads。

    Atomic

    Java knowledge-sharing

    Atomicity means that an operation cannot be interrupted, even if multiple threads are executed simultaneously。

    Visibility

    Visibility means whether the other threads are immediately known when a thread changes the value of the shared variable。

    Order

    The natural order of the process in the java memory model can be summed up in one sentence: if all operations are observed within this thread, all operations are sequenced; if another range is observed in one thread, all operations are unordered. The first sentence refers to the " threaded semantics within a thread " , while the second sentence refers to the " command reordering " phenomenon and the " work memory and master memory sync delay " phenomenon。

    The keywords volatile and synchronized are provided in java to ensure orderly operation between threads. Volatile contains semantics that prohibit the reordering of commands and ensures the visibility of different threads when operating with the same variable; synchronized keywords allow only one thread to operate with the same variable at the same time。

    Thread creation

    Succession to thread rewrite run() method

    Anonym internal category, i. E. New thread() after specific definition, where the run() method is rewritten

    Unable interface, rewrite run() method

    Differentiate run() and start() method for opening the thread

    Java knowledge-sharing

    Run() method cannot create a new thread, but calls the run() method in the current thread

    Start() method creates a new thread and calls its run-() method。

    Do not use stop()

    In general, threads end when implementation is complete and do not need to be closed manually, but we often create an infinity cycle backstage process to provide a service on an ongoing basis, so they need to be closed manually。

    There is also an api approach in jdk, such as stop() method, which is highly unrecommended because when called on, the stop() method would force the end of the route to half, which could cause data inconsistencies。

    But what should we do to stop an infinite cycle

    We recommend the addition of a boolean attribute to isstop in the class, and that the test isstop out of the loop and automatically terminates when the thread is completed, thus avoiding data inconsistencies。

    Wait() and notify()

    These two methods are not unique to the thread category, but are included in the parent type of the entire class。

    When an object moves to the water method, such as: objecta. Wait(), the current thread will wait on the object and release the object's lock until other threads use the objecta. Notify() method。

    It should be noted that both the wait and the notify methods must have access to the object's monitor (lock) and release the object's lock once the sync code is enforced, so they must be included in the object's synchronzied statement。

     
    ReportFavorite 0Tip 0Comment 0
    >Related Comments
    No comments yet, be the first to comment
    >SimilarEncyclopedia
    Featured Images
    RecommendedEncyclopedia