标签:


class Demo implements Runnable// extends Fu //准备扩展Demo类的功能,让其中的内容可以作为线程的任务执行{public void run() {show();}public void show() {for (int x = 0; x < 20; x++) {System.out.println(Thread.currentThread().getName() + "....." + x);}}}class ThreadDemo {public static void main(String[] args) {Demo d = new Demo();Thread t1 = new Thread(d);Thread t2 = new Thread(d);t1.start();t2.start();}}




class Ticket implements Runnable {private int num = 100;// Object obj = new Object();boolean flag = true;// 如果是真运行同步代码块,如果假运行同步函数?public void run() {// System.out.println("this:"+this);if (flag)while (true) {synchronized (this) {if (num > 0) {try {Thread.sleep(10);} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName()+ ".....obj...." + num--);}}}elsewhile (true)this.show();// 调用show方法}public synchronized void show() {// 将需要的放进来if (num > 0) {try {Thread.sleep(10);} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName()+ ".....function...." + num--);}}}class SynFunctionLockDemo {public static void main(String[] args) {Ticket t = new Ticket();// System.out.println("t:"+t);Thread t1 = new Thread(t);Thread t2 = new Thread(t);t1.start();try {Thread.sleep(10);} catch (InterruptedException e) {}t.flag = false;// 标记下t2.start();}}
class Bank {private int sum;// private Object obj = new Object();public synchronized void add(int num)// 同步函数{// synchronized(obj)// {sum = sum + num;// -->try {Thread.sleep(10);} catch (InterruptedException e) {}System.out.println("sum=" + sum);// }}}class Cus implements Runnable {private Bank b = new Bank();public void run() {for (int x = 0; x < 3; x++) {b.add(100);}}}class BankDemo {public static void main(String[] args) {Cus c = new Cus();Thread t1 = new Thread(c);Thread t2 = new Thread(c);t1.start();t2.start();}}
class Ticket implements Runnable {private static int num = 100;// Object obj = new Object();boolean flag = true;public void run() {// System.out.println("this:"+this.getClass());if (flag)while (true) {synchronized (Ticket.class)// (this.getClass())都行{if (num > 0) {try {Thread.sleep(10);} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName()+ ".....obj...." + num--);}}}elsewhile (true)this.show();}public static synchronized void show() {if (num > 0) {try {Thread.sleep(10);} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName()+ ".....function...." + num--);}}}class StaticSynFunctionLockDemo {public static void main(String[] args) {Ticket t = new Ticket();// Class clazz = t.getClass();// Class clazz = Ticket.class;// System.out.println("t:"+t.getClass());Thread t1 = new Thread(t);Thread t2 = new Thread(t);t1.start();try {Thread.sleep(10);} catch (InterruptedException e) {}t.flag = false;t2.start();}}
class Single {private static final Single s = new Single();private Single() {}public static Single getInstance() {return s;}}
class Single {private static Single s = null;private Single() {}public static Single getInstance() {if (s == null)// 多加一步,0进来以后创建完对象。多加一次判断1进来就不是空了就不判断了{synchronized (Single.class) {if (s == null)// -->0 -->1,0判断完空,创建对象,1进来就来不判断了,直接创建新对象,所以加同步// 这里用同步函数不好,用代码块好,提高效率s = new Single();}}return s;}}
class Ticket implements Runnable { // 这里有俩把锁this和objprivate int num = 100;Object obj = new Object();boolean flag = true;public void run() {if (flag)while (true) {synchronized (obj) {show();// 拿着obj想进this}}elsewhile (true)this.show();}public synchronized void show() {// 拿着this想进obj,互相拿着锁不放。和谐的时候是你进来我出去了synchronized (obj) // 同步函数加入同步代码块{if (num > 0) {try {Thread.sleep(10);} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName()+ ".....sale...." + num--);}}}}class DeadLockDemo {public static void main(String[] args) {Ticket t = new Ticket();// System.out.println("t:"+t);Thread t1 = new Thread(t);Thread t2 = new Thread(t);t1.start();try {Thread.sleep(10);} catch (InterruptedException e) {}t.flag = false; // 切换t2.start();}}
class Test implements Runnable {private boolean flag;Test(boolean flag) // 构造函数里赋值{this.flag = flag;}public void run() {if (flag) {while (true)synchronized (MyLock.locka) // a里有b{System.out.println(Thread.currentThread().getName()+ "..if locka....");synchronized (MyLock.lockb) {System.out.println(Thread.currentThread().getName()+ "..if lockb....");}}} else {while (true)synchronized (MyLock.lockb) { // b里有a{System.out.println(Thread.currentThread().getName()+ "..else lockb....");synchronized (MyLock.locka) {System.out.println(Thread.currentThread().getName()+ "..else locka....");}}}}}class MyLock {public static final Object locka = new Object();public static final Object lockb = new Object();}class DeadLockTest {public static void main(String[] args) {Test a = new Test(true);Test b = new Test(false);// 一般线程只有一个对象,因为多线程就是多个线程执行同一个任务,而这里FLAG的值只有俩种情况(真假)// 也是为了切换Thread t1 = new Thread(a);Thread t2 = new Thread(b);t1.start();t2.start();}}}
//资源class Resource {String name;String sex;}// 输入class Input implements Runnable {Resource r; // 参数传递// Object obj = new Object();Input(Resource r) {this.r = r;}public void run() {int x = 0;while (true) {synchronized (r)// 不能用this,obj,这里有俩个对象,得保证用一个唯一的对象,用参数的Class文件也可以,但是杀不用炮轰,所以用资源的参数就可以了{if (x == 0) {r.name = "mike";r.sex = "nan";} else {r.name = "丽丽";r.sex = "女女女女女女";}}x = (x + 1) % 2;}}}// 输出class Output implements Runnable {Resource r;// Object obj = new Object();Output(Resource r) {this.r = r;}public void run() {while (true) {synchronized (r) {System.out.println(r.name + "....." + r.sex);}}}}class ResourceDemo {public static void main(String[] args) {// 创建资源。Resource r = new Resource();// 创建任务。Input in = new Input(r);Output out = new Output(r);// 创建线程,执行路径。Thread t1 = new Thread(in);Thread t2 = new Thread(out);// 开启线程t1.start();t2.start();}}
//资源class Resource {String name;String sex;boolean flag = false;}// 输入class Input implements Runnable {Resource r;// Object obj = new Object();Input(Resource r) {this.r = r;}public void run() {int x = 0;while (true) {synchronized (r) {if (r.flag)try {r.wait();} catch (InterruptedException e) {}// r.wait俩帮小朋友玩游戏,你wait,另一帮不能叫醒你,一把锁if (x == 0) {r.name = "mike";r.sex = "nan";} else {r.name = "丽丽";r.sex = "女女女女女女";}r.flag = true;r.notify();}x = (x + 1) % 2;}}}// 输出class Output implements Runnable {Resource r;// Object obj = new Object();Output(Resource r) {this.r = r;}public void run() {while (true) {synchronized (r) {if (!r.flag)try {r.wait();} catch (InterruptedException e) {}System.out.println(r.name + "....." + r.sex);r.flag = false;r.notify();}}}}class ResourceDemo2 {public static void main(String[] args) {// 创建资源。Resource r = new Resource();// 创建任务。Input in = new Input(r);Output out = new Output(r);// 创建线程,执行路径Thread t1 = new Thread(in);Thread t2 = new Thread(out);// 开启线程t1.start();t2.start();}}
class Resource { // 将资源私有化,提高安全性private String name;private String sex;private boolean flag = false;public synchronized void set(String name, String sex) { // 同步写在这,需要同步就是这个if (flag)try {this.wait();} catch (InterruptedException e) {} // wait锁上的方法写在同步里this.name = name;this.sex = sex;flag = true;this.notify();}public synchronized void out() { // 同步写在这,需要同步就是这个if (!flag)try {this.wait();} catch (InterruptedException e) {}System.out.println(name + "...+...." + sex);flag = false;notify();}}// 输入class Input implements Runnable {Resource r;// Object obj = new Object();Input(Resource r) {this.r = r;}public void run() {int x = 0;while (true) {if (x == 0) {r.set("mike", "nan");} else {r.set("丽丽", "女女女女女女");}x = (x + 1) % 2;}}}// 输出class Output implements Runnable {Resource r;// Object obj = new Object();Output(Resource r) {this.r = r;}public void run() {while (true) {r.out();}}}class ResourceDemo3 {public static void main(String[] args) {// 创建资源。Resource r = new Resource();// 创建任务。Input in = new Input(r);Output out = new Output(r);// 创建线程,执行路径Thread t1 = new Thread(in);Thread t2 = new Thread(out);// 开启线程t1.start();t2.start();}}
class Resource {private String name;private int count = 1;private boolean flag = false;public synchronized void set(String name)//{while (flag)try {this.wait();} catch (InterruptedException e) {}// t1 t0 if的话 从这醒的,不再判断了this.name = name + count;// 烤鸭1 烤鸭2 烤鸭3count++;// 2 3 4System.out.println(Thread.currentThread().getName() + "...生产者..."+ this.name);// 生产烤鸭1 生产烤鸭2 生产烤鸭3flag = true;notifyAll();}public synchronized void out()// t3{while (!flag)try {this.wait();} catch (InterruptedException e) {} // t2 t3System.out.println(Thread.currentThread().getName() + "...消费者........"+ this.name);// 消费烤鸭1flag = false;notifyAll();}}class Producer implements Runnable {private Resource r;Producer(Resource r) {this.r = r;}public void run() {while (true) {r.set("烤鸭");}}}class Consumer implements Runnable {private Resource r;Consumer(Resource r) {this.r = r;}public void run() {while (true) {r.out();}}}class ProducerConsumerDemo {public static void main(String[] args) {Resource r = new Resource();Producer pro = new Producer(r);Consumer con = new Consumer(r);Thread t0 = new Thread(pro);Thread t1 = new Thread(pro);Thread t2 = new Thread(con);Thread t3 = new Thread(con);t0.start();t1.start();t2.start();t3.start();}}
import java.util.concurrent.locks.*; //如果不导包lock就写全称class Resource {private String name;private int count = 1;private boolean flag = false;// 创建一个锁对象。Lock lock = new ReentrantLock(); // 是lock接口的子类,自定义锁// lock 这个锁可以挂多个锁,通过已有的锁获取该锁上的监视器对象// Condition con = lock.newCondition(); //lock.newCondition(),lock的方法// 通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者。Condition producer_con = lock.newCondition();Condition consumer_con = lock.newCondition();public void set(String name)// t0 t1{lock.lock();try {while (flag)// try{lock.wait();}catch(InterruptedException e){}// t1 t0try {producer_con.await();} catch (InterruptedException e) {}// t1 t0this.name = name + count;// 烤鸭1 烤鸭2 烤鸭3count++;// 2 3 4System.out.println(Thread.currentThread().getName()+ "...生产者5.0..." + this.name);// 生产烤鸭1 生产烤鸭2 生产烤鸭3flag = true;// notifyAll();// con.signalAll();consumer_con.signal();} // 不准备处理异常,所以不需要catchfinally {lock.unlock();}}public void out()// t2 t3{lock.lock();try {while (!flag)// try{this.wait();}catch(InterruptedException e){} //t2 t3try {cousumer_con.await();} catch (InterruptedException e) {} // t2 t3System.out.println(Thread.currentThread().getName()+ "...消费者.5.0......." + this.name);// 消费烤鸭1flag = false;// notifyAll();// con.signalAll();producer_con.signal();} finally {lock.unlock();}}}class Producer implements Runnable {private Resource r;Producer(Resource r) {this.r = r;}public void run() {while (true) {r.set("烤鸭");}}}class Consumer implements Runnable {private Resource r;Consumer(Resource r) {this.r = r;}public void run() {while (true) {r.out();}}}class ProducerConsumerDemo2 {public static void main(String[] args) {Resource r = new Resource();Producer pro = new Producer(r);Consumer con = new Consumer(r);Thread t0 = new Thread(pro);Thread t1 = new Thread(pro);Thread t2 = new Thread(con);Thread t3 = new Thread(con);t0.start();t1.start();t2.start();t3.start();}}
class Demo {void show() {synchronized (this)//{wait();// t0 t1 t2全活了}}void method() {synchronized (this)// t4{// wait();notifyAll();}// t4}}

class StopThread implements Runnable {private boolean flag = true; // ture就转public synchronized void run() {while (flag)// 为什么经常在run方法写循环,因为短了也不值得开启多线程。不写while(ture){try {wait();// t0 t1} catch (InterruptedException e) {System.out.println(Thread.currentThread().getName() + "....."+ e);flag = false; // 强制把你们恢复回来,再读到标记就会结束掉。wait是等待不是结束}System.out.println(Thread.currentThread().getName() + "......++++");}}public void setFlag() {flag = false; // 把标记置为假}}class StopThreadDemo {public static void main(String[] args) {StopThread st = new StopThread();Thread t1 = new Thread(st);Thread t2 = new Thread(st);t1.start();t2.setDaemon(true); // setDameon是守护线程,可以理解为后台线程,你停我也停。前台必须手动结束t2.start();// 下边是主线程int num = 1;for (;;) // 无限循环{if (++num == 50) {// st.setFlag(); 把标记置为假,结束while循环t1.interrupt();// t2.interrupt(); //验证守护线程break; // 结束for循环}System.out.println("main...." + num);}System.out.println("over");}}


class Demo implements Runnable {public void run() {for (int x = 0; x < 50; x++) {System.out.println(Thread.currentThread().toString() + "....." + x);// tostring输出字符串,里面有名字、优先级(默认是5,常量是大写)Thread.yield(); // 会成对的出现,你一下我一下}}}class JoinDemo {public static void main(String[] args) throws Exception {Demo d = new Demo();Thread t1 = new Thread(d);Thread t2 = new Thread(d);t1.start();t2.start();// t2.setPriority(Thread.MAX_PRIORITY);// t1.join();//t1线程要申请加入进来,运行(主线程释放执行权和资格)。t1结束主线程才能执行,t2.join也一样,临时加入一个线程运算时可以使用join方法for (int x = 0; x < 50; x++) {// System.out.println(Thread.currentThread()+"....."+x);}}}
class ThreadTest {public static void main(String[] args) {new Thread(new Runnable() {public void run() {System.out.println("runnable run");}}) { // 有复写了父类的run方法,输出的是子类public void run() {System.out.println("subThread run"); // 输出的是子类}}.start();// 随时开辟一个线程,匿名内部类,这里有三个new Thread() {public void run() {for (int x = 0; x < 50; x++) {System.out.println(Thread.currentThread().getName()+ "....x=" + x);}}}.start();for (int x = 0; x < 50; x++) {System.out.println(Thread.currentThread().getName() + "....y=" + x);}Runnable r = new Runnable() {public void run() {for (int x = 0; x < 50; x++) {System.out.println(Thread.currentThread().getName()+ "....z=" + x);}}};new Thread(r).start();}}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4911786.html