Java Concurrent Programming 2

Source: Internet
Author: User

Description: First on the code, notes follow-up supplement
public class CoderTest1 {

private static Object key;

public static void Main (string[] args) {

Synchronization container Vector HashTable collections.synchronizedlist ()
If a modification occurs during an iteration, the concurrent modification exception modifies the thread independent copy of a collection (which can result in the inability to get the latest data, which has an effect on efficiency)
/* Multiple threading operations easy to throw arrayindexoutofboundsexception exceptions
* vector<object> Vector = new vector<> ();
int lastsize = Vector.size ()-1;
Vector.remove (lastsize);
* */
vector<object> vector = new vector<> ();
Synchronized (key) {
int lastsize = Vector.size ()-1;
Vector.remove (lastsize);
// }
This situation can occur with concurrent modification exceptions Concurrentmodificationexception
arraylist<object> list = new arraylist<> ();
List.add (1);
List.add (1);
List.add (1);
List.add (1);
List.add (1);
for (Object obj:list) {
System.out.println (obj);
List.remove (obj);
// }

Concurrent container CONCURRENTHASHMAP segment lock a lock on each linked list, after 1.8 it seems to be a node lock
Map.putifabsent only key does not exist to operate successfully and will not overwrite
Copyonwritearraylist/set Application Scenario: Read more, write very little
blocking queues
FIFO, block if queue is empty
Concurrenthashmap map = new Concurrenthashmap ();
(map.putifabsent);
Map.putifabsent (1,3);
SYSTEM.OUT.PRINTLN (map);
//
copyonwritearraylist<object> list = new copyonwritearraylist<> ();
Blockingqueue queue = new Arrayblockingqueue (2);

1 Latching 2 Fence 3 signal volume
After you reach the fence, you need all the threads to execute before you can continue executing other threads
Final Countdownlatch latch = new Countdownlatch (2);
First thread
New Thread () {
public void Run () {
try {
SYSTEM.OUT.PRINTLN ("Child Thread" +thread.currentthread (). GetName () + "executing");
Thread.Sleep (3000);
SYSTEM.OUT.PRINTLN ("Child Thread" +thread.currentthread (). GetName () + "execution complete");
Latch.countdown ();
} catch (Interruptedexception e) {
E.printstacktrace ();
// }
// }
}.start ();
A second thread
New Thread () {
public void Run () {
try {
SYSTEM.OUT.PRINTLN ("Child Thread" +thread.currentthread (). GetName () + "executing");
Thread.Sleep (3000);
SYSTEM.OUT.PRINTLN ("Child Thread" +thread.currentthread (). GetName () + "execution complete");
Latch.countdown ();
} catch (Interruptedexception e) {
E.printstacktrace ();
// }
// }
}.start ();
//
try {
System.out.println ("Wait for two sub-threads to finish executing ... ");
Atresia
Latch.await ();
System.out.println ("Two sub-threads have been executed ... ");
System.out.println ("Continue with the main thread");
} catch (Interruptedexception e) {
E.printstacktrace ();
// }

int N = 4;
Cyclicbarrier barrier = new Cyclicbarrier (N);
for (int i=0; i<n; i++) {
New Writer (barrier). Start ();
}
int worknum = number of 8;//workers
Semaphore Semaphore = new Semaphore (5);//semaphore, number of machines
for (int i=0; i<worknum; i++) {
New Worker (I,semaphore). Start ();
}
}

For fences
Static class Writer extends thread{
Private Cyclicbarrier Cyclicbarrier;
Public Writer (Cyclicbarrier cyclicbarrier) {
This.cyclicbarrier = Cyclicbarrier;
}
@Override
public void Run () {
SYSTEM.OUT.PRINTLN ("Thread" +thread.currentthread (). GetName () + "writing data ... ");
try {
Thread.Sleep (5000);
SYSTEM.OUT.PRINTLN ("Thread" +thread.currentthread (). GetName () + "write data, wait for other threads to write data ... ");
Cyclicbarrier.await ();
} catch (Interruptedexception e) {
E.printstacktrace ();
} catch (Brokenbarrierexception e) {
E.printstacktrace ();
}
System.out.println ("All threads have finished writing data, continue to perform other tasks ... ");
}
}

For signal volume
Static class Worker extends thread{
private int num;
Private Semaphore Semaphore;
public Worker (int num, Semaphore Semaphore) {
This.num = num;
This.semaphore = semaphore;
}
@Override
public void Run () {
try {
Semaphore.acquire ();
System.out.println ("Worker" +this.num+ "occupies a machine production ... ");
Thread.Sleep (3000);
SYSTEM.OUT.PRINTLN ("Workers" +this.num+ "release the Machine");
Semaphore.release ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

}

Java Concurrent Programming 2

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.