需要注意的是,run方法是一个一般方法,它只用来存线程要运行的代码,start方法才能让线程运行。
示例: class Demo extends Thread{ public void run(){ for(int x=0; x<100; x++) System.out.println("demo run----"+x); } } class ThreadDemo { public static void main(String[] args) { //创建好一个线程。 Demo d = new Demo(); //开启线程并执行该线程的run方法 d.start();。 //主线程运行的代码。 for(int x=0; x<100; x++) System.out.println("Hello World!--"+x); } }运行以上代码发现:自定义线程与主统一线程出现了随机运行的情况。这就是多线程的一个特性:随机性。谁抢到谁执行,至于执行多长,cpu说的算。发现自定义线程与主统一线程出现了随机运行的情况。这就是多线程的一个特性:随机性。谁抢到谁执行,至于执行多长,cpu说的算。要明确一点:在某一个时刻,只能有一个程序在运行(多核除外),cpu在做着快速的切换,看上去像是在同时运行一样。
<span style="white-space:pre"> </span>示例: //自定义一个类,实现Runnable接口,并覆写run方法。 class Test implements Runnable{ public void run(){ for(int a=0; a<100; a++){ System.out.println("Test--"+x); } } } class Thread_Runnable{ public static void main(String[] args){ //创建Thrread类的对像,将Test对像作传给Thread类的构造函数。 Thread t = new Thread(new Test()); //开启线程。 t.start(); } }
<span style="white-space:pre"> </span>定义示例: class Demo1{ public synchronized void method(){ System.out.println("我是同步函数。"); } }当同步函数用static修饰时
<span style="white-space:pre"> </span>定义示例: class Demo2{ public static synchronized void method(){ System.out.println("我是用static修饰的同步函数。"); } }(2).同步代码块
class Resource { private String name; private int count = 1;
private boolean flag = false; <span style="font-family: Arial, Helvetica, sans-serif;"> </span>
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition(); private Condition condition_con = lock.newCondition(); public void set(String name)throws InterruptedException{ lock.lock(); try{ while(flag) condition_pro.await();//t1,t2 this.name = name+"--"+count++; System.out.println(Thread.currentThread().getName()+"-----生产者---"+this.name); flag = true; condition_con.signal(); } finally{
<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//释放锁的动作一定要执行。</span> lock.unlock(); } } public void out()throws InterruptedException{ lock.lock(); try{ while(!flag) condition_con.await(); System.out.println(Thread.currentThread().getName()+"----消费者----"+this.name); flag = false; condition_pro.signal(); } finally{ lock.unlock(); } } } class Producer implements Runnable { private Resource res;
Producer(Resource res){ this.res = res; } public void run(){ while(true){ try{ res.set("+商品+"); } catch (InterruptedException e){
<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//只为演示代码,省略处理方式。</span>
<span style="white-space:pre"> </span>e.<span style="font-family: Arial, Helvetica, sans-serif;"><strong>printstackTrace();</strong></span><span style="white-space:pre"> </span> } } } } class Consumer implements Runnable{
private Resource res<span style="font-family: Arial, Helvetica, sans-serif;">;</span>
Consumer(Resource res){ this.res = res; } public void run(){ while(true){ try{ res.out(); } catch (InterruptedException e){
<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//只为演示代码,省略处理方式。</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span>e.</span><strong>printStackTrace</strong>
} } } }
<span style="white-space:pre"> </span>(1).饿汉式 示例: class Single{ private Single(){} private static Single s = new Single(); public static s getInstance(){ return s; } } (2).懒汉式 懒汉式也称作类的延时加载,是方法被调用时对像才初始化。 示例: class Single{ private Single(){} private Single s = null; public void static s getInstance(){ if(s==null){ synchronized(Single.class){ if(s==null){ s = new Single(); } } } return s; } }
原文地址:http://blog.csdn.net/lu_xiao_liang/article/details/45796855