Catch the problem when learning multithreading [转]

xiaoxiao2021-03-06  81

It's a problem that I encountered when I learned multithreaded?

source:

PConline author:

QLAMPSKYFACE

It has been a lot of time in contact with the multi-thread, but also a lot of things, but always feel that it is not so moving, when Debug, it will be more worried about what problems in synchronization, think of "Programmer" Written code "This sentence, I feel that it is not fake. I finally had one day, I think it is time to figure out this question, so I will find relevant content on the Internet, and I can't find this stage. It should be seen, it is not too simple, it is a strike, I don't know what the cloud. I finally almost figured out, there were many misunderstandings. I used to be very different from the truth. I think of my time spent. I really feel a bit more, so write it out, one is to prevent yours, I will forget it later, II is to give a point like I understand that I can refer to the stuff. Gossip less, transfer to the topic! - ------------------------------- First from the thread. There are two forms of thread creation: - ------------------------------- One is inheriting from the Thread class. THREAD class is a concrete class, ie Abstract class, which encapsulates the behavior of threads. To create a thread, the programmer must create a new class exported from the Thread class. The programmer completes useful work by overlying the run () function of Thread. The user does not call this function directly; but by calling the Start () function of Thread (), the function calls Run (). For example: public class test () {public test () {} public static void main (String args []) {test t1 = new test (); test t2 = new test (); t1.start (); t2.start (); Public void Run () {// do thread's things}} --------------------------- the other Runnable interface, this interface has only one function, Run (), this function must be implemented by a class that implements this interface.

For example: public class Test implements Runnable {Thread thread1; Thread thread2; public Test () {thread1 = new Thread (this, "1"); thread2 = new Thread (this, "2");} public static void main (String Args []) {test t = new test (); t.startthreads ();} public void run () {// do thread's things} (); thread1.start (); thread2.start (); }} The difference between the two creation methods is not large. The first kind of inherits from Thread, only the object is created, the second kind has to create the Thread object. But when you want to inherit a certain class, you can only use the last one. Most of the reasons why most people prefer to be here. ------------------------- Let's talk about Synchronized 4 Sype: 1. Method declaration, use, after the range operator (public, etc.), return to the type declaration (Void et al.). At this time, the thread gets the member lock, that is, only one thread can enter Method, other threads want to call this method at this time, can only queue wait, current thread (that is, the other threads can enter after the method is executed, for example: public synchronized void synmethod () { // Method} 2. Use a code block, Synchronized followed by parentheses, in parentheses is variables, so that only one thread enters the code block at a time, thread is a member lock. For example: public int synmethod INT A1) {synchronized (a1) {// only one thread enters} } 3. Synchronized is an object in brackets, at this time, the thread is subject to the object lock. For example: public class mythread implements runnable {public static void main (string args []) {Mythread Mt = new mythread (); Thread T1 = New Thread (MT, "T1"); Thread T2 = New Thread (MT, "T2"); Thread T3 = New Thread (MT, "T3"); Thread T4 = New Thread (MT, "T4"); Thread T5 = New Thread (MT, "T5"); Thread T6 = New Thread (MT, "T6"); T1.Start (); t2.start (); t3.start (); t4.start (); T5.Start (); T6.Start ();} public void run () {synchronized (this) {system.out.println (thread.currentthread (). getname ());

}}} For 3, if the thread is entered, you get the current object lock, then other threads cannot be performed any operation on all objects of the class. The object-level use lock is usually a relatively rough approach. Why do you want to lock the entire object without allowing other threads to use other synchronization methods in the object to access shared resources? If an object has multiple resources, it is not necessary to lock all threads outside only to let a thread use some of the resources. Since each object is locked, you can use the virtual object to lock: Class Finegraholdlock {MymemberClass X, Y; Object Xlock = New Object (), YLOCK = New Object (); public void foo () {synchronized ( XLOCK) {// access X here} // do sometying here - But don't use shared resources synchronized (ylock) {// access y here}} public void bar () {synchronized (this) {// access Both X And y Here} // do something here - But don't use shared resources}}} 4. Synchronized is a class. At this time, the thread is an object lock. For example: class arraywithlockorder {private static long num_locks = 0; Private long lock_order; private int [] arr; public arraywithlockorder (int [] a) {arr = a; synchronized (arraywithlockorder.class) {// ----- Here Num_locks ; // Lock number plus 1. Lock_order = num_locks; / / Set the unique Lock_Order for this object instance. }} Public long lockOrder () {return lock_order;} public int [] array () {return arr;}} class SomeClass implements Runnable {public int sumArrays (ArrayWithLockOrder a1, ArrayWithLockOrder a2) {int value = 0; ArrayWithLockOrder first = a1 // Reserved an arraywithlockorder last = A2; / / local copy of the array reference. INT size = a1.Array (). Length; if (size == a2.Array (). Length) {if (a1.lockorder ()> A2.lockorder ()) / / Determine and set the lock {//// order.

First = a2; LAST = a1;} synchronized (first) {// locked the object in the correct order.

Synchronized (last) {int [] arr1 = a1.Array (); int [] arr2 = a2.Array (); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-107885.html

New Post(0)