1.Thread实现:
1import java.util.Date; 2import java.text.SimpleDateFormat; 3 4public class MyThread extends Thread{ 5 @Override 6 public void run(){ 7 8 SimpleDateFormat strf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 9 String d = strf.format(new Date());// new Date()为获取当前系统时间 10 System.out.println(d+" "+Thread.currentThread().getName()); 11 12 } 13 14 public static void main(String[] args) { 15// MyThread myThread = new MyThread(); 16 for (int i = 0; i < 10; i++) { 17 new MyThread().start(); 18 19 } 20 21 } 22}
2020-01-23 21:15:54 Thread-1
2020-01-23 21:15:54 Thread-8
2020-01-23 21:15:54 Thread-7
2020-01-23 21:15:54 Thread-5
2020-01-23 21:15:54 Thread-4
2020-01-23 21:15:54 Thread-3
2020-01-23 21:15:54 Thread-0
2020-01-23 21:15:54 Thread-2
2020-01-23 21:15:54 Thread-6
2020-01-23 21:15:54 Thread-9
2.Runnable :
1class MyRunnable implements Runnable { 2 3 int i =0; 4 @Override 5 public void run(){ 6 System.out.println(Thread.currentThread().getName()+" "+ i++); 7 } 8 9 10 public static void main(String[] args) { 11 Runnable implRunnable = new MyRunnable(); 12// Thread thread = new Thread(implRunnable); 13 for (int i = 1; i <5 ; i++) { 14// new Thread(implRunnable).start(); // int i全局线程操作共享 15 new Thread(new MyRunnable()).start(); // int i 变量线程操作独立, 16 } 17 18 } 19}
3. Callable:
1import java.util.concurrent.Callable; 2import java.util.concurrent.ExecutionException; 3import java.util.concurrent.FutureTask; 4 5public class MyCallable implements Callable<Object> { 6 7 @Override 8 public Object call() throws Exception{ 9 int result =0 ; 10 for (int j = 0; j <8 ; j++) { 11 result +=j; 12 } 13 System.out.println(Thread.currentThread().getName()); 14 return result; 15 } 16 17 public static void main(String[] args) throws InterruptedException, ExecutionException { 18 for (int i = 0; i <5 ; i++) { 19 Callable<Object> callable = new MyCallable() ; 20 FutureTask<Object> futureTask = new FutureTask<Object>(callable); 21 new Thread(futureTask).start(); 22 System.out.println(futureTask.get()); 23 24 } 25 26 } 27 28}
Thread-0
28
Thread-1
28
Thread-2
28
Thread-3
28
Thread-4
28
4.ThreadPool使用同步原语,重入锁,同步代码块,代码类,或者对象,对基本数据类型无效:
1import java.util.concurrent.locks.ReentrantLock; 2 3public class SynchronizedTest implements Runnable { 4 private int i =10; 5 // synchronized mean block one thread opt some var util other th have fish some operation ; 6 @Override 7 public void run() { 8 //或者同步代码块 Integer Object ,int是基本数据类型,不是object; 9 //synchronized(SychronizeTest.class) { 10 ReentrantLock RLock = new ReentrantLock(); 11 RLock.lock(); 12 if (i >= 1) { 13 14 try { 15 Thread.sleep(100); 16 i--; 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 System.out.println(Thread.currentThread().getName() + " " + i); 21 } else { 22 System.out.println("库存不足"); 23 } 24 RLock.unlock(); 25 //} 26 } 27 public static void main(String[] args) throws Exception{ 28 Runnable runnable = new SynchronizedTest(); 29 for (int i = 0; i < 20; i++) { 30 new Thread(runnable).start(); 31 32 } 33 Thread.sleep(3000); 34 35 } 36}