java线程的典例

来源:互联网 发布:类似itools的软件 编辑:程序博客网 时间:2024/05/21 07:59
<pre name="code" class="java">/** * 创建一个简单的线程,通过继承Thread类 *  * @author Al菜菜 * */public class ThreadDemo01 extends Thread {private String name;public ThreadDemo01(String name) {super();this.name = name;}@Overridepublic void run() {for (int i = 0; i < 1000; i++) {System.out.println(name + ":" + i);}}public static void main(String[] args) {ThreadDemo01 td = new ThreadDemo01("me");ThreadDemo01 td1 = new ThreadDemo01("you");td.start();td1.start();}}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">/** * 通过实现Runnable接口,创建一个线程 *  * @author Al菜菜 * */public class ThreadDemo02 implements Runnable {private String name;public ThreadDemo02(String name) {this.name = name;}public static void main(String[] args) {ThreadDemo02 td = new ThreadDemo02("me");ThreadDemo02 td1 = new ThreadDemo02("you");Thread th=new Thread(td);Thread th1=new Thread(td1);th.start();th1.start();}@Overridepublic void run() {for (int i = 0; i < 1000; i++) {System.out.println(this.name + ":" + i);}}}


/** * 创建线程,通过实现Runnable接口进而实现资源的共享 *  * @author Al菜菜 * */public class ThreadDemo03 implements Runnable {private int ticket = 10;public static void main(String[] args) {ThreadDemo03 td1 = new ThreadDemo03();Thread t1 = new Thread(td1, "线程一");Thread t2 = new Thread(td1, "线程二");t1.start();t2.start();}@Overridepublic void run() {for (int i = 1; i <= 10; i++) {if (ticket > 0)System.out.println(Thread.currentThread().getName() + ticket--);}}}
/** * 判断线程是否启动 *  * @author Al菜菜 * */public class ThreadDemo04 implements Runnable {public static void main(String[] args) {ThreadDemo04 td4 = new ThreadDemo04();Thread t1 = new Thread(td4, "线程啊");System.out.println(t1.isAlive() ? "线程已经启动" : "线程还未启动");t1.start();System.out.println(t1.isAlive() ? "线程已经启动" : "线程还未启动");}@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < 10; i++) {System.out.println(i);}}}
/** * 线程的强制执行 *  * @author Al菜菜 * */public class ThreadDemo05 {public static void main(String[] args) {Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 1000; i++) {System.out.println(Thread.currentThread().getName() + ":"+ i);}}});t1.start();for (int i = 0; i < 1000; i++) {if (i > 10) {try {t1.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(Thread.currentThread().getName() + ":" + i);}}}


/** * 线程的休眠 * @author Al菜菜 * */public class ThreadDemo6 implements Runnable{public static void main(String[] args) {ThreadDemo6 t=new ThreadDemo6();new Thread(t).start();;}@Overridepublic void run() {for(int i=0;i<100;i++){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+":"+i);}}}

/** * 线程的打断 *  * @author Al菜菜 * */public class ThreadDemo07 {public static void main(String[] args) {Thread t1=new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(10000);} catch (InterruptedException e) {System.out.println("打断");}}});t1.start();try {Thread.sleep(2000);t1.interrupt();} catch (InterruptedException e) {e.printStackTrace();}}}
/** * 后台线程 * @author Al菜菜 * */public class ThreadDemo08 {public static void main(String[] args) {Thread t1=new Thread(new Runnable() {@Overridepublic void run() {while(true){System.out.println("后台线程");}}});t1.setDaemon(true);t1.start();}}
/** * 线程的优先级 * @author Al菜菜 * */public class ThreadDemo09 implements Runnable{public static void main(String[] args) {ThreadDemo09 td9=new ThreadDemo09();Thread t =new Thread(td9);Thread t1 =new Thread(td9);Thread t2 =new Thread(td9);t.setPriority(5);t1.setPriority(8);t2.setPriority(10);t.start();t1.start();t2.start();}@Overridepublic void run() {for(int i=0;i<100;i++){System.out.println(Thread.currentThread().getName()+":"+i);}}}


/** * 线程的礼让 * @author Al菜菜 * */public class ThreadDemo10 implements Runnable{public static void main(String[] args) {ThreadDemo10 td10=new ThreadDemo10();Thread th=new Thread(td10);Thread th1=new Thread(td10);th.start();th1.start();}@Overridepublic void run() {for(int i=0;i<100;i++){if(i%3==0){System.out.println("线程礼让");Thread.yield();}System.out.println(Thread.currentThread().getName()+":"+i);}}}

/** * 买票问题 * @author Al菜菜 * */public class ThreadDemo11 implements Runnable{private int a=10;public static void main(String[] args) {ThreadDemo11 t11 = new ThreadDemo11();Thread t1=new Thread(t11);Thread t2=new Thread(t11);Thread t3=new Thread(t11);t1.start();t2.start();t3.start();}@Overridepublic void run() {for(int i=0;i<50;i++){synchronized (this) {if(a>0){try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("a:"+a--);}}}}}
/** * 消费者和生产者的问题(比较好的实现方法,把要生产和消费的产品利用封装的特性包装在一个类中) *  * @author Al菜菜 * */public class ThreadDemo12 {public static void main(String[] args) {goods g=new goods();Product pro=new Product(g);Thread th1=new Thread(pro);th1.start();Customer cus=new Customer(g);Thread th2=new Thread(cus);th2.start();}}/** * 商品类 * @author Al菜菜 * */class goods {private String name;private int num;public void setName(String name) {this.name = name;}public String getName() {return name;}public void setNum(int num) {this.num = num;}public int getNum() {return num;}/** * 消费后将商品设为空值 */public void setEmpty(){this.name=null;this.num=0;}/** * 判断是否有商品可以消费 * @return */public boolean isEmpty(){return this.name==null&&this.num==0;}}class Product implements Runnable {private boolean flag = false;//用于循环生产商品private goods g;public Product(goods g) {this.g = g;}@Overridepublic void run() {for (int i = 0; i < 30; i++) {synchronized (Object.class) { //线程锁if(!g.isEmpty()){try {Object.class.wait();  //当有商品没有被消费是,进入等待状态} catch (InterruptedException e) {e.printStackTrace();}}if (flag) {System.out.print("生产了商品一");g.setName("商品一");try {Thread.sleep(100); //用于在不使用锁的情况下,触发现象} catch (InterruptedException e) {e.printStackTrace();}System.out.println("生产了20个");g.setNum(20);flag=false;} else {System.out.print("生产了商品二");g.setName("商品二");try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("生产了40个");g.setNum(40);flag=true;}Object.class.notify();}}}}class Customer implements Runnable {private goods g;public Customer(goods g) {this.g = g;}@Overridepublic void run() {for(int i=0;i<30;i++){synchronized (Object.class) {if(g.isEmpty()){try {Object.class.wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("消费商品:"+g.getName()+"数量:"+g.getNum());g.setEmpty();Object.class.notify();}}}}
/** * 用java线程实现死锁 * @author Al菜菜 * */public class ThreadDemo13 implements Runnable{public static void main(String[] args) {ThreadDemo13 td13 = new ThreadDemo13();Thread t1=new Thread(td13);Thread t2=new Thread(td13);t1.start();t2.start();}@Overridepublic void run() {synchronized (Object.class) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (this) {for(int i=0;i<100;i++){System.out.println(Thread.currentThread().getName()+":"+i);}}}synchronized (this) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (Object.class) {for(int i=0;i<100;i++){System.out.println(Thread.currentThread().getName()+":"+i);}}}}}





                                             
0 0
原创粉丝点击