Java多线程与并发库高级应用 学习笔记 1-9课
生活随笔
收集整理的這篇文章主要介紹了
Java多线程与并发库高级应用 学习笔记 1-9课
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
來源XXX,免得打廣告嫌疑。
?
http://www.cnblogs.com/whgw/archive/2011/10/03/2198506.html?
今天看了文章才發(fā)現(xiàn)創(chuàng)建線程最佳方式為實現(xiàn)Runnable接口,之前的習慣要改魯。
?
http://blog.csdn.net/imzoer/article/details/8500670
Java中Timer的用法
?
package timer;import java.util.Calendar; import java.util.Timer; import java.util.TimerTask;public class TimerTest {static Timer timer = new Timer();public static void main(String[] args) {// task task to be scheduled.// delay delay in milliseconds before task is to be executed.// period time in milliseconds between successive task executions.timer.schedule(new MyTimerTask(), 2000);}} /** 執(zhí)行間隔2秒與4秒交替進行*/ class MyTimerTask extends TimerTask {static boolean change = true;Calendar calendar = Calendar.getInstance();@Overridepublic void run() {change = !change;System.out.println(calendar.getTime());new Timer().schedule(new MyTimerTask(), change ? 2000 : 4000);}}?
線程安全的在于多線程對同一資源對象的讀、寫、事務(wù)完成階段。
?
http://www.cnblogs.com/mengdd/archive/2013/02/16/2913806.html
java?synchronized關(guān)鍵字詳解?
弄明白synchronized這個關(guān)鍵字在不同情況鎖住的對象粒度,以及范圍。
?
package Thread;/*** 子線程2次,主線程4次交替循環(huán)10次。**/ public class AlternateLoop {static Sysout sysout = new Sysout();public static void main(String[] args) {for (int i = 0; i < 10; i++) {sysout.subBusiness(i);sysout.mainBusiness(i);}}}// class MyThread implements Runnable { // @Override // public void run() { // AlternateLoop.sysout.subBusiness(1); // } // } // // class MyThread2 implements Runnable { // // @Override // public void run() { // AlternateLoop.sysout.mainBusiness(i); // } // }/** 同一資源管理業(yè)務(wù)邏輯*/ class Sysout {private boolean isSub = true;public synchronized void mainBusiness(int i) {while (isSub) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}for (int j = 0; j < 4; j++) {System.out.println("mainBusiness" +j+"loop of"+ i);}isSub = true;this.notify();}public synchronized void subBusiness(int i) {while (!isSub) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}for (int j = 0; j < 2; j++) {System.out.println("subBusiness" +j+"loop of"+ i);}isSub = false;this.notify();} }?
Java并發(fā)編程:深入剖析ThreadLocal?
每個線程存在對應(yīng)的線程副本對象。
?
package Thread;import java.util.Random;public class ThreadLocalTest {public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();public static void main(String[] args) {for (int i = 0; i < 2; i++) {new Thread(new Runnable() {@Overridepublic void run() {int data=new Random().nextInt();System.out.println(Thread.currentThread().getName()+"has put data :"+data);threadLocal.set(data);new A().get();new B().get();}}).start();}}static class A {public void get() {int data=threadLocal.get();System.out.println("A from Thread" + Thread.currentThread().getName() + ""+ data);}}static class B {public void get() {int data=threadLocal.get();System.out.println("B from Thread" + Thread.currentThread().getName() + ""+ data);}} }上面的例子需要改造一下 這種內(nèi)部靜態(tài)類用起來不科學啊。
?
java線程:Atomic(原子的)? jdk 1.5的新特性?
?
Java并發(fā)編程:線程池的使用
package Thread;import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit;public class ThreadPoolTest {public static void main(String[] args) {ExecutorService threadPool =Executors.newCachedThreadPool(); // ExecutorService threadPool =Executors.newFixedThreadPool(10); // ExecutorService threadPool = Executors.newSingleThreadExecutor();for (int i = 0; i < 10; i++) {final int task = 1;threadPool.execute(new Runnable() {@Overridepublic void run() {for (int j = 0; j < 10; j++) {try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+ "is looping of " + j + "for task of" + task);}}});}Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("bombing!");}}, 6, 2, TimeUnit.SECONDS);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/linkarl/p/4808838.html
總結(jié)
以上是生活随笔為你收集整理的Java多线程与并发库高级应用 学习笔记 1-9课的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Caching和Purgeable Me
- 下一篇: Spring+Hibernate配置多数