线程(1)

(1)

package day20150907thread;
/**
 * 线程调度分配给cpu的时间片段给不同的线程
 * 得到时间片段的线程被cpu运行,其他线程等待
 * 线程调度会尽可能均匀的分配时间片段给不同的进程
 * 
 * 经纪人分配给剧组不同的档期,让葛大爷去演戏
 * 线程调度分配给不同的线程时间片段,让cpu去运行
 */
public class ThreadDemo1 {
    /**
     * 第一种创建线程的方式
     * 继承Thread类,重写run方法
     * 
     * 写起来简单,但用起来没有实现Runnable灵活
     */
    public static void main(String[] args) {

        //有先后顺序的运行方式叫同步运行(同一个线程)
        //各干各的叫异步运行(多个线程里执行)
        Thread t1 = new Thread1();
        Thread t2 = new Thread2();
        /**
         * start方法用于将线程纳入线程调度
         * 这时,线程处于runnable状态
         * 等待线程调度分配时间片段
         * 当线程调度将时间片段给配给当前线程
         * 该线程的run方法才被执行
         * 直到run方法执行完毕,线程结束最终被回收
         * 在线程的run方法执行期间,该线程处于走走停停
         */
        t1.start();
        t2.start();
    }

}

//Thread:实现类,已经实现Runnable接口
class Thread1 extends Thread{

    @Override
    public void run() {
        for(int i=0;i<5000;i++){
            System.out.println("谁啊");
        }
    }

}
class Thread2 extends Thread{
    @Override
    public void run(){
        for(int i=0;i<5000;i++){
            System.out.println("我");
        }
    }
}

(2)

package day20150907thread;
/**
 * 第二种创建线程方式:
 * 定义线程体Runnable
 */
public class ThreadDemo2 {

    public static void main(String[] args) {
        Runnable r1 = new MyRunnable1();
        Runnable r2 = new MyRunnable2();
//      r1.run();
//      r2.run();

        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
    }

}

class MyRunnable1 implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<5000;i++){
            System.out.println("谁啊?");
        }

    }

}

class MyRunnable2 implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<5000;i++){
            System.out.println("我");
        }

    }

}

(3)

package day20150907thread;

public class ThreadDemo3 {
/**
 * 使用匿名内部类的方式创建线程
 */
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            public void run(){
                for(int i=0;i<5000;i++){
                    System.out.println("11111111111");
                }
            }
        };

        Thread t2 = new Thread(
                new Runnable(){
                    public void run(){
                        for(int i=0;i<5000;i++){
                            System.out.println("22");
                        }
                    }
                }
        );

        t1.start();
        t2.start();

    }
}

(4)

package day20150907thread;
/**
 * 获取执行当前代码片段的线程
 * 可以调用Thread提供的静态方法
 * Thread Thread.currentThread()
 */
public class ThreadDemo4 {

    public static void main(String[] args) {
        Thread current = Thread.currentThread();
        System.out.println("main线程:"+current);
        testCurrentThread();

        Thread t1 = new Thread(){
            public void run(){
                Thread myt = Thread.currentThread();
                System.out.println("myt:"+myt);
                testCurrentThread();
            }
        };
        t1.start();
    }

    public static void testCurrentThread(){
        Thread t = Thread.currentThread();
        System.out.println("方法线程:"+t);
    }

}

(5)

package day20150907thread;
/**
 * ID:非空并且唯一
 * 线程其他API
 */
public class ThreadDemo5 {

    public static void main(String[] args) {
        //获取调用main方法的相关信息

        Thread t = Thread.currentThread();

        //获取线程id,通常由系统分配
        long id = t.getId();
        System.out.println("ID"+id);
        /**
         * 获取线程名字
         * 格式为Thread-X
         * 但是main方法的线程不是这个,为main
         */
        String name = t.getName();
        System.out.println("线程名字:"+name);//main

        int p = t.getPriority();
        System.out.println("线程优先级:"+p);

        System.out.println("线程状态"+t.getState());
        System.out.println("线程是否活着:"+t.isAlive());
        System.out.println("是否后台进程:"+t.isDaemon());
        System.out.println("线程是否被中断:"+t.isInterrupted());
    }

}

(6)

package day20150907thread;
/**
 * 线程优先级
 * 1-10
 * 理论上,线程优先级高的线程
 * 被分配的时间片段次数多
 */
public class ThreadDemo6 {

    public static void main(String[] args) {
        Thread max = new Thread(){
            public void run(){
                for(int i=0;i<5000;i++){
                    System.out.println("maxmaxmaxmaxmaxmaxmaxmax");
                }
            }
        };

        Thread min = new Thread(){
            public void run(){
                for(int i=0;i<5000;i++){
                    System.out.println("min");
                }
            }
        };

        Thread nor = new Thread(){
            public void run(){
                for(int i=0;i<5000;i++){
                    System.out.println("normal");
                }
            }
        };

        max.setPriority(Thread.MAX_PRIORITY);//或者max.setPriority(10);
        min.setPriority(Thread.MIN_PRIORITY);//或者min.setPriority(1);
        max.start();
        min.start();
        nor.start();

    }

}

(7)

package day20150907thread;
/**
 * 后台线程(守护线程)
 * 当进程中的所有前台线程都结束时,后台线程结束
 * 无论后台线程是否还在运行
 */
public class ThreadDemo7 {

    public static void main(String[] args) {
        //rose:前台进程
        Thread rose = new Thread(){
            public void run(){
                for(int i=0;i<8;i++){
                    System.out.println("Go");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        //jack:守护进程
        Thread jack = new Thread(){
            public void run(){
                while(true){
                    System.out.println("Jack: you go, I go!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        rose.start();
        jack.setDaemon(true);
        jack.start();


        //while(true);//此死循环没执行完的话,main方法不结束,守护进程也不结束

        //main方法执行完
        System.out.println("main方法执行完了");
    }

}

(8)

package day20150907thread;

import java.text.SimpleDateFormat;
import java.util.Date;

//(8)sleep 阻塞
public class ThreadDemo8 {
    /**
     * 实现电子表(15:13:24)
     */
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        while(true){
            Date now = new Date();
            System.out.println(sdf.format(now));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

(9)

package day20150907thread;
/**
 * join:线程同步
 * 
 * 注:
 * 解决多线程并发安全问题的办法是:
 * 将异步的操作变成同步的
 * 
 * 产生多线程并发的原因是:
 * 多线程并发操作同一数据
 */
public class ThreadDemo9 {
    /**
     * 图片是否下载完
     */
    public static boolean isFinish;
    public static void main(String[] args) {
        final Thread download = new Thread(){
            public void run(){
                System.out.println("开始下载图片。。。");
                for(int i=1;i<=100;i++){
                    System.out.println(i+"%");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("下载完成");
                isFinish = true;
            }
        };
        /*
         * main方法中定义了一个内部类show
         * 该内部类中若想引用main方法中的其他局部变量
         * 那么这个变量必须是final的
         */
        Thread show = new Thread(){         
            public void run(){
                System.out.println("开始显示图片");
                //这里等待图片下载完成
                try {
                    download.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(!isFinish){
                    throw new RuntimeException("图片还没有下载完成");
                }
                System.out.println("图片打开");
            }
        };


        download.start();       
        show.start();

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值