java线程

luckyMan

贡献于2014-08-04

字数:3339 关键词: Java开发

public class Eat extends Thread{ @Override public void run() { for(int i=0;i<1000;i++){ try { Thread.sleep(100);//每隔1秒;交出线程占用CPU时间一秒钟。 System.out.println("吃饭"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class Music extends Thread{ @Override public void run() { for(int i=0;i<1000;i++){ try { Thread.sleep(100); System.out.println("听音乐"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class Demo1 { /** * 听音乐 */ private static void music(){ for(int i=0;i<1000;i++){ System.out.println("听音乐"); } } /** * 吃饭 */ private static void eat(){ for(int i=0;i<1000;i++){ System.out.println("吃饭"); } } public static void main(String[] args) { /*music(); eat();*/ /** * 利用多线程实现一边吃饭一边听歌 */ Music musicThread=new Music(); Eat eatThread=new Eat(); musicThread.start(); eatThread.start(); } } public class Thread1 extends Thread{ private int baoZi=1; private String threadName; public Thread1(String threadName) { super(); this.threadName = threadName; } @Override public void run() { while(baoZi<=10){ System.out.println(threadName+" 吃第"+baoZi+"包子"); baoZi++; } } public static void main(String[] args) { System.out.println("张三,李四一起吃包子,每人吃了10个"); Thread1 t1=new Thread1("张三线程"); Thread1 t2=new Thread1("李四线程"); t1.start(); t2.start(); } } public class Thread2 implements Runnable{ private int baoZi=1; private String threadName; public Thread2(String threadName) { super(); this.threadName = threadName; } @Override public synchronized void run() { while(baoZi<=10){ System.out.println(threadName+" 吃第"+baoZi+"包子"); baoZi++; } } public static void main(String[] args) { /*System.out.println("张三,李四一起吃包子,每人吃了10个"); Thread2 t1=new Thread2("张三线程"); Thread2 t2=new Thread2("李四线程"); Thread t11=new Thread(t1); Thread t12=new Thread(t2); t11.start(); t12.start();*/ Thread2 t1=new Thread2("超级张三线程"); Thread t11=new Thread(t1); Thread t12=new Thread(t1); Thread t13=new Thread(t1); // 实现资源共享 t11.start(); t12.start(); t13.start(); } } 线程线程1.新建 2.就绪 3.运行 4.阻塞 5.死亡 public class Demo1 implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++){ try { Thread.sleep(100); // 获取当前线程 Thread t=Thread.currentThread(); System.out.println(t.getName()+":"+i); // 返回线程的名称 if(i==5){ System.out.println("线程礼让:"); Thread.currentThread().yield(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Demo1 demo1=new Demo1(); Thread t1=new Thread(demo1); new Thread(demo1).start(); new Thread(demo1,"线程3").start(); System.out.println("t1是否活动:"+t1.isAlive()); t1.start(); System.out.println("t1是否活动:"+t1.isAlive()); Thread t2=new Thread(demo1,"线程A"); Thread t3=new Thread(demo1,"线程B"); Thread t4=new Thread(demo1,"线程C"); t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.MIN_PRIORITY); t4.setPriority(Thread.NORM_PRIORITY); t2.start(); t3.start(); t4.start(); } } public class Demo1 implements Runnable{ private int baoZi=10; @Override public void run() { // TODO Auto-generated method stub while(baoZi>0){ System.out.println(Thread.currentThread().getName()+":吃了第"+baoZi+"个包子"); baoZi--; } } // /** * 同步方法 */ public synchronized void run() { // TODO Auto-generated method stub while(baoZi>0){ System.out.println(Thread.currentThread().getName()+":吃了第"+baoZi+"个包子"); baoZi--; } } // public void run() { /** * 同步块 */ synchronized (this) { while(baoZi>0){ System.out.println(Thread.currentThread().getName()+":吃了第"+baoZi+"个包子"); baoZi--; } } } public static void main(String[] args) { Demo1 demo1=new Demo1(); new Thread(demo1,"张三").start(); new Thread(demo1,"李四").start(); new Thread(demo1,"王五").start(); } }

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ]
5 人已下载

下载文档

相关文档