java 线程
编程技术  /  houtizong 发布于 3年前   35
class Ticket extends Thread{private int tickets = 100;public void run() {while(true) {if(tickets > 0) {System.out.println(Thread.currentThread().getName() + "...sales_" + tickets--);}}}}
public class ThreadDemo_1 {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();}}
class Ticket implements Runnable{private int tickets = 100;public void run() {while(true) {if(tickets > 0) {System.out.println(Thread.currentThread().getName() + "...sales_" + tickets--);}}}}
public class ThreadDemo_3{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();}}
class Ticket implements Runnable{private int tickets = 100;public void run() {while(true) {if(tickets > 0) {try{Thread.sleep(10);}catch (InterruptedException e){e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "...sales_" + tickets--);}}}}
class Ticket implements Runnable{private int tickets = 100;Object object = new Object();public void run() {while(true) {synchronized(object) {if(tickets > 0) {try{Thread.sleep(10);}catch (InterruptedException e){e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "...sales_" + tickets--);}}}}}
class Ticket implements Runnable{private int tickets = 100;Object object = new Object();public void run() {while(true) {show();}}public synchronized void show() {if(tickets > 0) {try{Thread.sleep(10);}catch (InterruptedException e){e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "...sales_" + tickets--);}}}
class Ticket implements Runnable{private int tickets = 100;Object obj = new Object();boolean flat = true;public void run() {if(flat) {while(true) {synchronized(obj) {if(tickets > 0) {try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName() + "...sales..." + tickets--);}}}} else { show(); }}public synchronized void show() {while(true) {if(tickets > 0) {try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName() + "...show_" + tickets--);}}}}
public class ThisLockDemo{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 (Exception e){}t.flat = false;t2.start();}}
class Ticket implements Runnable{private static int tickets = 100;boolean flat = true;public void run() {if(flat) {while(true) {synchronized(this) {if(tickets > 0) {try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName() + "...sales..." + tickets--);}}}} else { show(); }}public static synchronized void show() {while(true) {if(tickets > 0) {try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName() + "...show_" + tickets--);}}}}
class Test implements Runnable{//定义布尔变量flag,对线程进行判断,让其执行相应的代码块private boolean flag;public Test(boolean flag) {this.flag = flag;}//使用两个锁对象,对两个代码块都进行锁定,而且是相互锁定public void run() {//根据线程的布尔属性执行不同的代码块//两个锁对象是相互嵌套的if(flag) {synchronized(DeadLockTest.locka) {System.out.println("if locka");synchronized(DeadLockTest.lockb) {System.out.println("if lockb");}}} else {synchronized(DeadLockTest.lockb) {System.out.println("else lockb");synchronized(DeadLockTest.locka) {System.out.println("else locka");}}}}}
public class DeadLockTest {static Object locka = new Object();static Object lockb = new Object();public static void main(String[] args) {//建立两个布尔属性不一样的线程,并启动Thread t1 = new Thread(new Test(true));Thread t2 = new Thread(new Test(false));t1.start();t2.start();}}
class People{private String name;private String sex;boolean flag = false;public synchronized void set(String name, String sex) {if(flag)try{this.wait();}catch(InterruptedException e) {}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;this.notify();}}
class Input implements Runnable{private People p;public Input(People p) {this.p = p;}int i = 0;public void run() {while(true) {if(i == 0) {p.set("nike","male");} else {p.set("丽丽","女");}i = (i+1)%2;}}}
class Output implements Runnable{private People p;public Output(People p) {this.p = p;}public void run() {while(true) {p.out();}}}
public class InputOutputDemo_2{public static void main(String[] args) {People p = new People();new Thread(new Input(p)).start();new Thread(new Output(p)).start();}}
class Product{private String name = "";private int count = 0;boolean flag = false;public synchronized void set(String name) {while(flag) //*********try{this.wait();}catch(InterruptedException e) {}this.name = name + "..." + count++;System.out.println(Thread.currentThread().getName() + "...生产者" + this.name);flag = true;this.notifyAll();}public synchronized void out() {while(!flag)//********try{this.wait();}catch(InterruptedException e) {} System.out.println(Thread.currentThread().getName() + "...消费者......" + this.name);flag = false;this.notifyAll();}}
class Producer implements Runnable{private Product pro;public Producer(Product pro) {this.pro = pro;}public void run() {while(true) {pro.set("商品");}}}
class Consumer implements Runnable{private Product pro;public Consumer(Product pro) {this.pro = pro;}public void run() {while(true) {pro.out();}}}
class ProducerConsumerTest {public static void main(String[] args) {Product pro = new Product();Producer p = new Producer(pro);Consumer c = new Consumer(pro);Thread t1 = new Thread(p);Thread t2 = new Thread(p);Thread t3 = new Thread(c);Thread t4 = new Thread(c);t1.start();t2.start();t3.start();t4.start();}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接