标签:ati 幸运 lag static 判断 this 接口 改变 jpg
package java012; /** * 2017/9/29 * 说明: */ class Fu{ } class Demo extends Fu { private String name; public Demo(String name){ this.name = name; } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+Thread.currentThread().getName()); } } } public class ThreadDemo { public static void main(String[] args) { } }
package java012; /** * 2017/9/29 * 说明: */ class Fu{ } class Demo extends Fu implements Runnable { private String name; public Demo(String name){ this.name = name; } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+Thread.currentThread().getName()); } } @Override public void run() { this.show(); } } public class ThreadDemo { public static void main(String[] args) { } }
package java012; /** * 2017/9/29 * 说明: */ class Fu{ } class Demo extends Fu implements Runnable { private String name; public Demo(String name){ this.name = name; } public void show(){ for(int x =0;x<10;x++){ System.out.println(name+":"+x+":线程的名称:"+Thread.currentThread().getName()); } } @Override public void run() { this.show(); } } public class ThreadDemo { public static void main(String[] args) { Thread t1 = new Thread(); Thread t2 = new Thread(); t1.start(); t2.start(); } }
public Thread(Runnable target)
package java012; /** * 2017/9/29 * 说明: */ class Fu{ } class Demo extends Fu implements Runnable { public void show(){ for(int x =0;x<10;x++){ System.out.println(x+":线程的名称:"+Thread.currentThread().getName()); } } @Override public void run() { this.show(); } } public 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(); } }
package java012; /** * 2017/9/30 * 说明: */ class Ticket extends Thread { private int num = 100; public void sale(){ while(true){ if(num > 0){ System.out.println(Thread.currentThread().getName()+":"+num--); } } } @Override public void run() { this.sale(); } } public class TicketDemo { public static void main(String[] args) { Ticket t1 = new Ticket(); Ticket t2 = new Ticket(); Ticket t3 = new Ticket(); Ticket t4 = new Ticket(); t1.start(); t2.start(); t3.start(); t4.start(); } }
package java012; /** * 2017/9/30 * 说明: */ class Ticket extends Thread { private static int num = 100; public void sale(){ while(true){ if(num > 0){ System.out.println(Thread.currentThread().getName()+":"+num--); } } } @Override public void run() { this.sale(); } } public class TicketDemo { public static void main(String[] args) { Ticket t1 = new Ticket(); Ticket t2 = new Ticket(); Ticket t3 = new Ticket(); Ticket t4 = new Ticket(); t1.start(); t2.start(); t3.start(); t4.start(); } }
package java012; /** * 2017/9/30 * 说明: */ class Ticket implements Runnable { private int num = 100; public void sale(){ while(true){ if(num > 0){ System.out.println(Thread.currentThread().getName()+":"+num--); } } } @Override public void run() { this.sale(); } } public class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket(); Ticket tt = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); Thread t3 = new Thread(tt); Thread t4 = new Thread(tt); t1.start(); t2.start(); t3.start(); t4.start(); } }
package java016; class Ticket implements Runnable { private int num = 100; @Override public void run() { while(true) { if(num > 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+num--); } } } } public class ThreadDemo { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); Thread t3 = new Thread(t); Thread t4 = new Thread(t); t1.start(); t2.start(); t3.start(); t4.start(); } }
synchronized(对象){ //需要同步的代码块 }
package java012; /** * 2017/9/30 * 说明: */ class Ticket implements Runnable { private int num = 100; public void sale(){ while(true){ synchronized (this){ if(num > 0){ try { Thread.sleep(10); } catch (InterruptedException e) { } System.out.println(Thread.currentThread().getName()+":"+num--); } } } } @Override public void run() { this.sale(); } } public class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); Thread t3 = new Thread(t); Thread t4 = new Thread(t); t1.start(); t2.start(); t3.start(); t4.start(); } }
package java012; /** * 2017/9/30 * 说明: */ class Ticket implements Runnable { private int num = 100; public void sale(){ Object obj = new Object(); while(true){ synchronized (obj){ if(num > 0){ try { Thread.sleep(10); } catch (InterruptedException e) { } System.out.println(Thread.currentThread().getName()+":"+num--); } } } } @Override public void run() { this.sale(); } } public class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); Thread t3 = new Thread(t); Thread t4 = new Thread(t); t1.start(); t2.start(); t3.start(); t4.start(); } }
package java012; /** * 2017/10/4 * 说明: */ class Bank{ private int sum ; public void add(int num){ sum += num; try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("存储了"+sum+"元"); } } class Customer implements Runnable{ private Bank b = new Bank(); @Override public void run() { for(int x =0;x<3;x++){ b.add(100); } } } public class BankDemo { public static void main(String[] args) { Customer c = new Customer(); Thread t1 = new Thread(c); Thread t2 = new Thread(c); t1.start(); t2.start(); } }
package java012; import java.util.concurrent.SynchronousQueue; /** * 2017/10/4 * 说明: */ class Bank{ private int sum ; private Object obj = new Object(); public void add(int num){ synchronized (obj){ sum += num; try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("存储了"+sum+"元"); } } } class Customer implements Runnable{ private Bank b = new Bank(); @Override public void run() { for(int x =0;x<3;x++){ b.add(100); } } } public class BankDemo { public static void main(String[] args) { Customer c = new Customer(); Thread t1 = new Thread(c); Thread t2 = new Thread(c); t1.start(); t2.start(); } }
package java012; import java.util.concurrent.SynchronousQueue; /** * 2017/10/4 * 说明: */ class Bank{ private int sum ; public synchronized void add(int num){ sum += num; try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("存储了"+sum+"元"); } } class Customer implements Runnable{ private Bank b = new Bank(); @Override public void run() { for(int x =0;x<3;x++){ b.add(100); } } } public class BankDemo { public static void main(String[] args) { Customer c = new Customer(); Thread t1 = new Thread(c); Thread t2 = new Thread(c); t1.start(); t2.start(); } }
package java012; /** * 2017/10/5 * 说明: */ class Ticket implements Runnable{ private int num = 100; boolean flag = true; @Override public void run() { if(flag){ while (true){ synchronized (this){ if(num >0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+num--); } } } }else{ while(true){ sale(); } } } private synchronized void sale() { if(num >0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+num--); } } } public class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); t1.start(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } t.flag = false; t2.start(); } }
package java012; /** * 2017/10/5 * 说明: */ class Ticket implements Runnable{ private static int num = 100; boolean flag = true; @Override public void run() { if(flag){ while (true){ synchronized (Ticket.class){ if(num >0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+num--); } } } }else{ while(true){ sale(); } } } private synchronized static void sale() { if(num >0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+num--); } } } public class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); t1.start(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } t.flag = false; t2.start(); } }
class Singleton{ private static Singleton singleton = new Singleton(); private Singleton(){} public static Singleton getInstance(){ return singleton; } }
class Singleton{ private static Singleton singleton = null; private Singleton(){} public static Singleton getInstance(){ if(singleton == null){ singleton = new Singleton(); } return singleton; } }
class Singleton{ private static Singleton singleton = null; private Singleton(){} public static Singleton getInstance(){ synchronized (Singleton.class){ if(singleton == null){ singleton = new Singleton(); } } return singleton; } }
package java012; /** * 2017/10/5 * 说明: */ class Ticket implements Runnable{ private int num = 400; boolean flag = true; private Object obj = new Object(); @Override public void run() { if(flag){ while (true){ synchronized (obj){ sale(); } } }else{ while(true){ sale(); } } } private synchronized void sale() { synchronized (obj){ if(num >0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+num--); } } } } public class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); t1.start(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } t.flag = false; t2.start(); } }
标签:ati 幸运 lag static 判断 this 接口 改变 jpg
原文地址:http://www.cnblogs.com/xuweiweiwoaini/p/7613508.html