线程的学习---1

package com.lesson18;

public class TestThread {

    /**
     * @param args
     */

    public static void main(String[] args) {


        // TODO Auto-generated method stub


        // 线程的运行-- 当t.start();的时候就有个线程开始啦 但不一定


会很快执行


        MyThread t = new MyThread();

        t.setDaemon(true);// ---这是后台程序--前台是看不见的但是


在运行的


        t.start();



        // 没有他们的执行的顺序-- 要看cpu的算法-- 才能够执行



        // 多线程--执行


        // new MyThread().start();


        // new MyThread().start();


        // new MyThread().start();


        // new MyThread().start();


        //

        while (true) {


            try {


                Thread.sleep(1000);


                // 联合的线程执行


                // t.join();这里就是去先执行MyThread这个里的程序


                t.join(2000);// --设置时间的长短


            } catch (Exception e) {

            }

            System.out.println("MyThread--main");


        }

    }

}











package com.lesson18;


//一定要执行Thread--而且要有run的方法


public class MyThread extends Thread {


    public void run() {


        while (true) {


            // 异常处理啦-- 防止出错


            try {


                Thread.sleep(1000);


            } catch (Exception e) {


                System.out.println("4444");


            }

            System.out.println("MyThread---" + getName());


            System.out.println("MyThread---" + getId());


            System.out.println("MyThread---" + getPriority());


        }


    }

}












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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值