Section I: The basic concept of process and thread one: Process concept 1. The process of executing a program (a task). (program refers to the application--such as QQ, player, etc.) Note: The process is dynamic, the file placed in the disk file is not a process, only in the running state can be called a process. Three states: Load, run, end the program: is the use of code to manipulate data. 2. Processes hold resources (shared memory, shared files) and threads. Note: Processes are the carrier of resources and threads. 3. Characteristic A: The process is the basic unit of the system running program. B: Each process has its own separate piece of memory space, a set of system resources. (System resources such as some files, etc.) C: The internal data and state of each process are completely independent.
Two: Threading concept-threads are multiple execution paths for a program 1, threads are the smallest unit of execution in the system 2, multiple threads in the same process (running a program is a process) 3, a resource for a thread-sharing process
4, the interaction of threads: Multiple threads need the correct interaction to work. Note: There are two mutually exclusive and synchronous modes of interaction. 5, the use of multithreading purposes: reduce the execution time of the program —— —— section II: Creating lines Cheng: Java language Thread support 1, Java support for threading is embodied in two: Class Thread (Class) and interface Runnable (interface), in java.la The common approach to Ng's Package 2, Class Thread, and interface Runnable is that public void run () provides us with code 3 that threads actually work, thread common method join: Suspends execution of the currently executing thread, waits for the call to the After the thread of the method is finished, the thread continues to execute. Yield (): The current thread yields the cup, again to the virtual machine call, to the same or higher priority threads, allocating the cup resources. (though it is possible to redistribute it to itself) 4, several forms of overloading a: There are no parameters, indicating that other threads must wait for the executing thread to complete, and they will get the chance to run. B:nanos is to change the accuracy, can be changed to Nanosecond 5, the start of the thread, can not be started with the Run method, Otherwise, it will be no different from the ordinary object.
Two: Thread creation and startup 1, two ways to create A thread A: Inherits thread class Mythread extends thread{};//by inheriting threads class Thread mythread = new Mythread (), Mythread. Start ();//The Run () method is executed after a thread starts
B: Create an implementation class thread myrunnable = new Thread (new Myrunnable) by implementing the Runnable interface class Myrunnable implements runnable{}//; The implementation class object of this class is passed into Myrunnable.start ();
C: Summary 1, if the implementation interface through the Thread.CurrentThread () GetName () method to obtain the current thread name, inheritance thread GetName () method to get the current thread name. 2, at the same time, the same processor or the same core can only run a single thread, when one thread sleeps, another line Cheng our processor Time 3, the thread's start () method is invoked, the threads start, and the JVM automatically invokes the thread's run () method to perform the task. The Run method appears at the top of the standalone execution space. When the thread has finished executing, the thread terminates. 4, high priority thread, it is possible to occupy CPU priority, not necessarily.
2. The method of the thread class A:.getname () returns the name of the thread B:.setname (String name) to change the thread's name C:.start () so that the thread starts executing, and the Java virtual machine invokes the thread's Run method. D:.sleep (long Millis) allows the currently executing thread to hibernate (suspend execution) within the specified number of milliseconds, which is affected by the system timer and scheduler precision and accuracy. The thread does not lose ownership of any monitor. E:.currentthreat () returns a reference to the currently executing thread object. (static method, returning thread type) —— —— section III: Thread synchronization One: concurrency--multithreading disadvantages 1, concurrency issues can cause competition, competitive state can cause data damage. 2, the occurrence of concurrency, from, if two different execution space (thread) on the method, the same object to perform read-change command.
Two: Synchronization 1, because of the problem of concurrency, we need to add some methods to lock. 2, plus the Synchronized keyword, representing the method is synchronized, after a single thread execution, the entire method before the other thread can not enter the method. 3, the object even if there are multiple synchronized methods, but there is still only a lock, a key. Therefore, if a thread enters the synchronization method of the object, the other thread will not be able to enter any synchronization method on the object until the lock is unlocked. (Because one object has only one key) Note: The lock of an object only works on the synchronization method. (The lock is not on the method, but on the object.) 4, if the object has two synchronization methods, then on behalf of two threads can not enter the same synchronization method, and can not enter the different synchronization methods. (Want to enter, only wait for a single thread to perform the synchronization of the hair, the key to return the object) Note: Use the synchronization method, generally this method, has the operation of the object member variables. Third: Deadlock 1, the situation is that two threads hold each other waiting for something, that is, the key. 2, because the method of synchronization, the thread if no key can not enter. 3, as long as there are two threads and two objects, you can throw a deadlock.
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.