-
How to use Threads:
public static void Main (string[] args) {Runnable r = ()->{while (true) {System.out.println ("thi S is thread "); } }; Thread t = new Thread (r); T.start (); while (true) {System.out.println ("This is main"); }}
Step: Implement the Run method of the Runnable interface, create a thread, and execute the Start method. Do not call the Run method, which only executes a task in one thread and does not create a new thread.
2. Middle Thread break
Runnable r = ()->{int i=0; while (! Thread.CurrentThread (). isinterrupted ()) {//Determines whether the current thread is terminated System.out.println ("This is Thread"); i++; if (i==100) {Thread.CurrentThread (). interrupt (); Gets the current thread and sends an interrupt request to it, and the interrupt state of the thread is set to true}};
3. Thread Status
1) Join () waits for the specified thread to terminate
2) getState () Get the status of this thread
4. Thread Properties
1) Each thread has a priority, and by default a thread inherits its parent thread's priority level.
2) The minimum priority is 1, the maximum is 10, the default is 5, you can set the priority by Thread.setpriority ().
3) Thread.setdaemon (true) converts a thread to a daemon, and when only the daemon is left, the virtual machine exits. Daemons should never access intrinsic resources, such as files, databases, and so on.
4) The Run method cannot throw any of the checked exceptions, but the non-checked exception causes the process to terminate. When a thread dies, the exception is passed to a processor for an uncaught exception.
5. Synchronization
1) Lock object: The Unlock statement must be in finally, and the try statement with the resource cannot be used.
private int acount=10000;public Lock lock = new Reentrantlock ();p ublic void Addcount () {lock.lock (); try {acount + = 1000; System.out.println (meg+ "Plus account is:" +acount); } finally {Lock.unlock (); }}
2) Conditional object (condition variable)
A lock can have one or more related conditional objects, the Newcondition method obtains a conditional object, calls the await () method to cause the current thread to be blocked, and discards the lock , putting the thread into the waiting set of the condition. The call to await () should be in the while loop so that another thread can come in.
Signalall () unlocks the blocking state of all threads in the waiting set for that condition.
3) Synchronized
Each object in Java has an internal lock with an internal condition, and if a method is declared with synchronized, the lock of the object will protect the entire method. Use Wait, Notifyall, notify.
Declaring a static method as synchronized is also legal, and the method obtains an internal lock on the related class, and no other thread can invoke this or any other synchronous static method of the same class.
4) lock
Trylock () attempts to acquire a lock without blocking and returns true if successful.
Reentrantreadwritelock rwl = new Reentrantreadwritelock () constructs a read-write lock object
Lock Readlock = Rwl.readlock () get read lock
Lock Writelock = Rwl.writelock (); Get write lock
6. Actuator
If you create a lot of short-life threads in your program, you should use the thread pool, a thread pool that contains many idle threads that are ready to run, and the Runnable object to the thread pool, and there will be threads that call the Run method, and the thread will not die when the Run method exits. Instead, you are ready to service the next request in the pool.
The Actuator (Executor) class has many static factory methods to build the thread pool.
Executorservice pool = Executors.newcachedthreadpool ();//returns the thread pool with the cache executorservice pool = Executors.newfixedthreadpool (10);//Specifies the number of threads executorservice pool = Executors.newsinglethreadexecutor ();//A single thread executes a task sequentially task = new Task (1,pool); future<?> res = pool.submit (Task); Perform task Pool.shutdown (); Close Service
This article is from the "Red Xiao" blog, please be sure to keep this source http://cnslp.blog.51cto.com/11387491/1920227
Java Fundamentals-Thread concurrency