class Test { public static void main(String[] args) { MyThread thread = new MyThread();//创建好了一个线程 thread.start(); // thread.start(); // thread.start(); } } class MyThread extends Thread { public int sum = 100; /* 为什么要覆盖run方法? */ public void run(){ while(true){ System.out.println(Thread.currentThread().getName()+"----"+sum--); } } }
public class TestThread2 { public static void main(String[] args) { MyRunner runner = new MyRunner();//创建要被执行的代码对象 Thread thread = new Thread(runner);//创建线程时要明确要执行的代码 thread.start(); thread.start(); thread.start(); } } class MyRunner implements Runnable { public int sum = 100; public void run() { sychronized(对象){//实现同步效果 if (sum > 0) { while (true ) { System. out.println(Thread.currentThread().getName() + "----" + sum++); } } } } }
package xyxysjxy.thread; public class TestThread3 { public static void main(String args[]) { MyRunner2 mrs1 = new MyRunner2(); Thread t1 = new Thread(mrs1); t1.start(); try { Thread. sleep(10); } catch (Exception e) { } mrs1. flag = true ; Thread t2 = new Thread(mrs1); t2.start(); } } class MyRunner2 implements Runnable { public int ticket = 100; public Object o = new Object(); public boolean flag = false; @Override public void run() { if (ticket > 0) { if (flag ) { while (true ) { if (ticket > 0) synchronized (o ) {//this才可以同步 try{Thread}catch(Exception e){} System. out.println(Thread.currentThread().getName() + "*****" + ticket--); } } } else { if(ticket > 0) show(); } } } public synchronized void show() { System. out.println(Thread.currentThread().getName() + "***" + ticket --); } }
package xyxysjxy.thread; public class TestThread4 { public static void main(String[] args) { Thread t1 = new Thread() { @Override public void run() { Demo D = Demo.getTestThread (); } }; t1. start(); Thread t2 = new Thread() { @Override public void run() { Demo D = Demo.getTestThread (); } }; t2. start(); } } class Demo { public static int flag = 0; private static Demo t = null; private Demo() { } public static Demo getTestThread() { if (t == null) { try { Thread. sleep(100); } catch (Exception e) { } flag++; System. out.println("flag = " + flag); t = new Demo(); return t ; } return t ; } }
package xyxysjxy.thread; public class TestThread4 { public static void main(String[] args) { Thread t1 = new Thread() { @Override public void run() { Demo D = Demo.getTestThread (); } }; t1.start(); Thread t2 = new Thread() { @Override public void run() { Demo D = Demo.getTestThread (); } }; t2.start(); } } class Demo { public static int flag = 0; private static Demo t = null; private Demo() { } public static Demo getTestThread() { if (t == null) {//为了高效点要进行两重判断,线程越安全,效率越低 synchronized (Demo.class) {// 不能用this,因为静态中不存在thi if (t == null) { try { Thread. sleep(100); } catch (Exception e) { } flag++; System. out.println("flag = " + flag); t = new Demo(); return t ; } } } return t ; } } 线程死锁:持有对方的锁 package xyxysjxy.thread; public class TestThread5 { public static void main(String[] args) { MyRunner3 mr3 = new MyRunner3(false); MyRunner3 mr4 = new MyRunner3( true); Thread t1 = new Thread(mr3); t1.start(); Thread t2 = new Thread(mr4); t2.start(); } } class MyRunner3 implements Runnable { private boolean b ; public MyRunner3( boolean b) { this.b = b; } @Override public void run() { if (b ) { synchronized (Suo.s1 ) { System. out.println("if suo si" ); synchronized (Suo.s2 ) { System. out.println("if suo s2"); } } } else { synchronized (Suo.s2 ) { System. out.println("else suo s2" ); synchronized (Suo.s1 ) { System. out.println("else suo s1"); } } } } } class Suo { static Suo s1 = new Suo(); static Suo s2 = new Suo(); }
package xyxysjxy.thread; public class ProducerConsumerDemo { public static void main(String args[]) { Resource r = new Resource(); Producer p1 = new Producer(r); Thread t1 = new Thread(p1); //Producer p2 = new Producer(r); Thread t3 = new Thread(p1); Consumer c1 = new Consumer (r); Thread t2 = new Thread(c1); //Consumer c2 = new Consumer(r); Thread t4 = new Thread(c1); t1.start(); t2.start(); t3.start(); t4.start(); } } class Resource { private int count = 1; private String name; private boolean flag = false; public synchronized void set(String name) { while (true ) {//if不能达到判断多线程效果 while (flag ) try { wait(); } catch (Exception e) { } this.name = name + "----" + count++; System. out.println("producer生产---" + this.name ); flag = true ; notifyAll();//notify不一定是唤醒对方的可能是自己这边的线程 } } public synchronized void out() { while (true ) { while (!flag ) try { wait(); } catch (Exception e) { } System. out.println("consumer消费-------" + name); flag = false ; notifyAll(); } } } class Producer implements Runnable { private Resource r; public Producer(Resource r) { this.r = r; } public void run() { r.set( "商品"); } } class Consumer implements Runnable { private Resource r; public Consumer(Resource r) { this.r = r; } public void run() { r.out(); } }
class Resource2 { private int count = 1; private String name; private boolean flag = false; private Lock lock = new ReentrantLock(); private Condition producer_c = lock.newCondition(); private Condition consumer_c = lock.newCondition(); public void set(String name) { lock.lock(); while (true ) { while (flag ) try { producer_c.await(); this.name = name + "----" + count++; System. out.println("producer生产---" + this. name); flag = true ; } catch (Exception e) { } finally { consumer_c.signal(); } } } public void out() { lock.lock(); while (true ) { while (!flag ) try { wait(); System. out.println("consumer消费-------" + name); flag = false ; } catch (Exception e) { } producer_c.signal(); } } }
package xyxysjxy.thread; public class TestThread6 { public static void main(String[] args) throws InterruptedException { MyRunner6 m = new MyRunner6(); Thread t1 = new Thread(m); t1.start(); Thread. sleep(100); t1.interrupt(); //当调用该方法时,会抛出interruptedException } } class MyRunner6 implements Runnable { @Override public synchronized void run() { boolean flag = true; System. out.println(flag); while (flag) {//设置标志位,目的是让运行的线程能得到控制 try { wait(); } catch (InterruptedException e) { System. out.println(e); flag = false;//把标志位至为false就可以中断线程 } System. out.println(Thread.currentThread().getName() + "------"); } } }
守护线程:后台线程 |
原文地址:http://blog.csdn.net/u011218159/article/details/26092023