线程
线程1
创建线程的第一种方法
public class TestThread { public static void main(String[] args) { A a = new A(); TestThread t = new TestThread(); a.start();//执行完成就重新创建了一条线程,自动调用run() a.display(); a.display1(); t.display3(); } public void display3(){ System.out.println("DDDDDDDD"); } } class A extends Thread{ public void run(){//重写Thread的方法 System.out.println("AAAAAA"); } void display(){ System.out.println("BBBBB"); } void display1(){ System.out.println("CCCCC"); } }
运行结果
第二种方法
public class TestThread { public static void main(String[] args) { A a = new A(); TestThread t = new TestThread(); Thread tt = new Thread(a);// Thread的构造方法 Thread(Runnable r)分配一个新的Thread对象 tt.start();//执行完成就重新创建了一条线程,自动调用run() a.display(); a.display1(); t.display3(); } public void display3(){ System.out.println("DDDDDDDD"); } } class A implements Runnable{ public void run(){//重写Thread的方法 System.out.println("AAAAAA"); } void display(){ System.out.println("BBBBB"); } void display1(){ System.out.println("CCCCC"); } }
Join
public class TestJoin { public static void main(String[] args){ join jj = new join("xx"); jj.start(); try { jj.join();//和下一语句的线程合并 }catch(Exception e){ return; } join j = new join("t"); Thread t = new Thread(); //Thread.currentThread().start(); currentThread(),返回对当前正在执行的线程对象的引用,main线程都还没执行 //不能返回当前对象,应该先调用start()方法,才会开辟线程 j.start(); for(int i = 0;i<100;i++) { System.out.println(Thread.currentThread().getName()+i+i);//当前正在执行的线程对象的引用的线程名称 try { Thread.currentThread().sleep(1000); }catch(Exception e){ return; } } } } class join extends Thread{ join(String s){ super(s); System.out.println(this.getName()); System.out.println(Thread.currentThread().getName()); } public void run(){ for(int i = 0;i<100;i++){ System.out.println(Thread.currentThread().getName()+i); try{ Thread.sleep(1000); }catch(Exception e){ return; } } } }
/* 停止线程的方法 pop() */ public class TestThread1 { public static void main(String[] args){ B b = new B(); V v = new V(); v.start(); Thread t = new Thread(b);//目前的理解 因为把b这个引用传给了Thread类,所以相当于Thread有了两个引用,一个指向原来的 //另一个指向B类的对象,所以t.start()也会调用B类里的run方法,Thread类被看作一个线程来创建的,任何一个线程都实现了Runnable类, // Thread也实现了Runnable,所以这么做也是新建一个线程,只不过是java自带的线程类而已。 t.start(); t.setName("xxxx"); for(int i =0;i<100;i++){ if(i%10==0){ System.out.println(Thread.currentThread().getName()+i); } try{ t.sleep(100); }catch(Exception e){ return; } b.pop(); } } } class B implements Runnable{ boolean flag = true; public void run(){ for(int i = 0;i<100;i++){ try{ Thread.sleep(100); }catch(Exception e){ return; } if(i%10==0&&flag==true) { System.out.println(Thread.currentThread().getName()+i); } } } public void pop(){ flag = false; } } class V extends Thread{ public void run(){ for(int i = 0;i<10;i++) { System.out.println(Thread.currentThread().getName()); } } }
this.getName() 和Thread.currentThread().getName()的区别
前者是拿到当前对象的名字,后者是拿到当前线程的名字线程
线程同步---synchronized
public class TestSynchronized { public static void main(String[] args){ shz s = new shz(); Thread t = new Thread(s); Thread tt = new Thread(s); t.setName("mm"); tt.setName("oo"); t.start(); tt.start(); } } class shz implements Runnable{ D d = new D(); public void run(){ d.display(); } } class D{ public static int num = 0; public void display(){ synchronized(this) {//锁住当前对象,让别的线程访问不了,直到访问当前对象的线程结束 num++; try { Thread.sleep(1000); } catch (Exception e) { } System.out.println(Thread.currentThread().getName() + "是第" + num + "个取钱的"); } } }
运行结果图
public class TestSynchronized { public static void main(String[] args){ shz s = new shz(); Thread t = new Thread(s); Thread tt = new Thread(s); t.setName("mm"); tt.setName("oo"); t.start(); tt.start(); } } class shz implements Runnable{ D d = new D(); public void run(){ d.display(); } } class D{ public static int num = 0; public void display(){ //锁住当前对象,让别的线程访问不了,直到访问当前对象的线程结束 num++; try { Thread.sleep(1000); } catch (Exception e) { } System.out.println(Thread.currentThread().getName() + "是第" + num + "个取钱的"); } }
运行结果图
/* 买票第一种方法 */ public class ticket { public static void main(String[] args) { window w = new window(); Thread t = new Thread(w); Thread t1 = new Thread(w); t.start(); t1.start(); } } class window implements Runnable { public int ticket = 100; //public String str = new String("aaa"); public void run() { while (true) { synchronized(this) { if (ticket > 0) { System.out.printf("%s站台正在出售第%d张票\n", Thread.currentThread().getName(), ticket); ticket--; } else { break; } } } } }
/* 买票第二种方法 */ public class ticket { public static void main(String[] args) { window w = new window(); window w1 = new window(); w.start(); w1.start(); } } class window extends Thread { public static int ticket = 100;//必须加static public static String str =new String("aaa");//必须加static public void run() { while (true) { synchronized(str) { if (ticket > 0) { System.out.printf("%s站台正在出售第%d张票\n", Thread.currentThread().getName(), ticket); ticket--; } else { break; } } } } }
案例生产消费
/** * 生产消费程序 */ public class TestPC { public static void main(String[] args) { PynStack p = new PynStack(); consumption c = new consumption(p); production pp = new production(p); Thread t = new Thread(c); Thread t1 = new Thread(pp); t.start(); t1.start(); } } class PynStack { int cent = 0; char[] arr = new char[8]; public synchronized void pop(char c) { while (cent == arr.length) { try { this.wait();//等待 } catch (Exception e) { return; } } this.notify();//叫醒 arr[cent] = c; System.out.printf("%s这条生产线正在生产%c\n", Thread.currentThread().getName(), c); cent++; } public synchronized char top() { char ce; while (cent == 0) { try { this.wait(); } catch (Exception e) { } } this.notify(); ce = arr[cent-1]; System.out.printf("%s这条出售线正在出售%c\n", Thread.currentThread().getName(), ce); cent--; return ce; } } class production implements Runnable { // public PynStack p = new PynStack(); public PynStack p = null; production(PynStack p){ this.p = p; } public void run() { for(int i = 0;i<20;i++) { p.pop((char)('a'+i)); } } } class consumption implements Runnable { //public PynStack p = new PynStack(); public PynStack p = null; consumption(PynStack p){ this.p = p; } public void run() { for(int i = 0;i<20;i++) { try{ Thread.sleep(2000); }catch(Exception e){ } p.top(); } } } 运行结果 Thread-1这条生产线正在生产a Thread-1这条生产线正在生产b Thread-1这条生产线正在生产c Thread-1这条生产线正在生产d Thread-1这条生产线正在生产e Thread-1这条生产线正在生产f Thread-1这条生产线正在生产g Thread-1这条生产线正在生产h Thread-0这条出售线正在出售h Thread-1这条生产线正在生产i Thread-0这条出售线正在出售i Thread-1这条生产线正在生产j Thread-0这条出售线正在出售j Thread-1这条生产线正在生产k Thread-0这条出售线正在出售k Thread-1这条生产线正在生产l Thread-0这条出售线正在出售l Thread-1这条生产线正在生产m Thread-0这条出售线正在出售m Thread-1这条生产线正在生产n Thread-0这条出售线正在出售n Thread-1这条生产线正在生产o Thread-0这条出售线正在出售o Thread-1这条生产线正在生产p Thread-0这条出售线正在出售p Thread-1这条生产线正在生产q Thread-0这条出售线正在出售q Thread-1这条生产线正在生产r Thread-0这条出售线正在出售r Thread-1这条生产线正在生产s Thread-0这条出售线正在出售s Thread-1这条生产线正在生产t Thread-0这条出售线正在出售t Thread-0这条出售线正在出售g Thread-0这条出售线正在出售f Thread-0这条出售线正在出售e Thread-0这条出售线正在出售d Thread-0这条出售线正在出售c Thread-0这条出售线正在出售b Thread-0这条出售线正在出售a 进程已结束,退出代码为
内存图分析