标签:
来源XXX,免得打广告嫌疑。
http://www.cnblogs.com/whgw/archive/2011/10/03/2198506.html
今天看了文章才发现创建线程最佳方式为实现Runnable接口,之前的习惯要改鲁。
http://blog.csdn.net/imzoer/article/details/8500670
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); } } /* * 执行间隔2秒与4秒交替进行 */ class MyTimerTask extends TimerTask { static boolean change = true; Calendar calendar = Calendar.getInstance(); @Override public void run() { change = !change; System.out.println(calendar.getTime()); new Timer().schedule(new MyTimerTask(), change ? 2000 : 4000); } }
线程安全的在于多线程对同一资源对象的读、写、事务完成阶段。
http://www.cnblogs.com/mengdd/archive/2013/02/16/2913806.html
弄明白synchronized这个关键字在不同情况锁住的对象粒度,以及范围。
package Thread; /** * 子线程2次,主线程4次交替循环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); // } // } /* * 同一资源管理业务逻辑 */ 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(); } }
每个线程存在对应的线程副本对象。
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() { @Override public 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); } } }
上面的例子需要改造一下 这种内部静态类用起来不科学啊。
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() { @Override public 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() { @Override public void run() { System.out.println("bombing!"); } }, 6, 2, TimeUnit.SECONDS); } }
标签:
原文地址:http://www.cnblogs.com/linkarl/p/4808838.html