java-Processes and processes

Source: Internet
Author: User

    • Processes and Threads

A process is the process of executing a program (task), which is dynamic; holding resources (shared memory, shared files) and threads, is the carrier of resources and threads.

A thread is the smallest execution unit in the system, with multiple threads in the same process and threads sharing the resources of the process.

Threads interact in ways that involve mutual exclusion and synchronization.

    • Common Methods of threading

Java support for threading is mainly embodied in the class thread and interface runnable, which inherit the Java.lang package, there is a common way to run ()

    • Method of Thread Stop error: Stop (), interrupt ()
    • The correct way to stop a thread
 Public classArmyrunnableImplementsRunnable {//volatile ensures that threads can correctly read values written by other threads    volatile BooleanKeeprunning =true; @Override Public voidrun () { while(keeprunning) {//launch five combos .             for(inti = 0; I < 5; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ "attacking each other [" + i + "]"); }            //PauseThread.yield (); } System.out.println (Thread.CurrentThread (). GetName ()+ "End of battle"); }}
    • Race condition

When multiple threads share access to the same data (memory area) at the same time, each thread attempts to manipulate the data, causing the data to be corrupted (corrupted), a phenomenon known as a race condition.

    • Thread Interaction: Mutex and synchronization

Mutex: At the same time, only one thread can operate on our critical data or critical section.

Mutex implementation: sysnchronized (intrinsic lock), sysnchronized is equivalent to adding a lock to the code so that other threads cannot access our critical resources in this critical area.

Synchronization: is a communication mechanism between threads, because some of the conditions of one thread are not available, so that other threads are in some kind of waiting state, and then because of the condition, one thread will wake up the other thread in some way.

Synchronization implementation: Wait ()/notify ()/notifyall ()--object the member method of the object

Wait set is the thread's lounge

 Public voidTransferintFromintTo,Doubleamount) {        //by synchronized the keyword to achieve mutual exclusion, synchronized can appear both on the method and in the form of a block in the method body//Mutual exclusion by lockobj lock implementation//lock operation is overhead, and multiple lock-in operations can degrade system performance        synchronized(lockobj) {//while loop, the task is blocked by conditions when the condition is not satisfied, rather than continuing to compete for CPU resources             while(Energyboxes[from] <amount) {                Try {                    //The condition is not satisfied, and the current thread is placed in the wait set on the lock object (lockobj)//wait set is the thread's loungelockobj.wait (); } Catch(interruptedexception e) {e.printstacktrace ();            }} System.out.println (Thread.CurrentThread (). GetName ()); Energyboxes[from]-=amount; System.out.printf ("Transfer%10.2f unit energy from%d to%d", from, amount, to); Energyboxes[to]+=amount; System.out.printf ("Sum of energy:%10.2f%n", Gettotalenergies ()); //Wake up all the threads waiting on the Lockobj objectLockobj.notifyall (); }            }
    • Extended

Java Memory Mode

Locks & Condition

Thread safety: atomicity and visibility ...

A common interactive model for multithreaded programming

Concurrent programming tools in JAVA5

    • Reference books

Core Java

Java Concurrency in practice

java-Processes and processes

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.