The problem solution of multithreading concurrency
Package map;
Import Java.util.HashMap;
Import java.util.Hashtable;
Import Java.util.Map;
public class Testconcurrentmap {
static int index;
public static void Main (string[] args) {
Execute ();
}
public static synchronized void execute () {
Final Map<integer, integer> Map = new Hashmap<integer, integer> ();
for (int i = 0; i < i++) {
New Thread (New Runnable () {
public void Run () {
Map.put (index++, index);
while (true) {
System.out.println (Thread.CurrentThread (). GetName () + ":" +map);
try {
Thread.Sleep (1000);
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}). Start ();
}
}
}
----------------------------------------------------------------------------------------------------------
@Test
public void Testisclose4wbdmultithread () {
SYSTEM.OUT.PRINTLN ("Multithreaded test begins ..... ");
Runnable task = new Runnable () {
@Override
public void Run () {
try {
SYSTEM.OUT.PRINTLN ("Entering task task");
Tradinghourserviceprovider Provider = Tradinghourserviceprovider.get ();
Boolean B = provider.isclose4wbd (Dateutil.todate ("2017-09-06", Dateutil.pattern_date2));
System.out.println (b);
Map<cachetype, string> cache = Configcache.get (). GetCache ();
} catch (Generictsexception e) {
E.printstacktrace ();
}
}
};
Map<thread, string> map = new hashmap<thread,string> ();
for (int i =0;i<20;i++) {
Thread t = new thread (Task);
T.currentthread (). SetName ("thread" +i);
Map.put (T, T.getname ());
}
set<thread> Threadset = Map.keyset ();
for (Thread T:threadset) {
T.start ();
}
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Multithreaded test end ..... ");
}
@Test
public void Testhashmapmultithread () {
SYSTEM.OUT.PRINTLN ("Multithreaded test begins ..... ");
Runnable task = new Runnable () {
@Override
public void Run () {
try {
SYSTEM.OUT.PRINTLN ("Entering task task");
Tradinghourserviceprovider Provider = Tradinghourserviceprovider.get ();
Boolean B = provider.isclose4wbd (Dateutil.todate ("2017-09-06", Dateutil.pattern_date2));
System.out.println (b);
Map<cachetype, string> cache = Configcache.get (). GetCache ();
} catch (Generictsexception e) {
E.printstacktrace ();
}
}
};
Map<thread, string> map = new hashmap<thread,string> ();
for (int i =0;i<8;i++) {
Thread t = new thread (Task);
T.currentthread (). SetName ("thread" +i);
Map.put (T, T.getname ());
}
set<thread> Threadset = Map.keyset ();
for (Thread T:threadset) {
T.start ();
}
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Multithreaded test end ..... ");
}
The problem solution of multithreading concurrency