JAVA线程Thread

package characteristic;

public class ThreadTest{
    public static void main(String[] args) {
        
    }
}


class ThreadMgSoldier extends Thread{
    public boolean isFlag = true;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("美国士兵来到战场!");
    }
    
    /*public ThreadMgSoldier(String name) {
        // TODO Auto-generated constructor stub
        super(name);
    }*/
}


class ThreadMgGeneral  extends Thread{
    public boolean isFlag = true;
    int count = 0;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(isFlag){
            System.out.println("美国将军来到了战场!" + count);
            Thread.yield();
            count++;
            /*if(count == 95){
                isFlag = false;
            }*/
        }
    }
    
}


class ThreadZhSoldier extends Thread{
    public boolean isFlag = true;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("中国士兵来到战场!");
    }
    /*public ThreadZhSoldier(String name) {
        // TODO Auto-generated constructor stub
        super(name);
    }*/
}

class ThreadZhGeneral extends Thread{
    public boolean isFlag = true;
    int count = 0;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(isFlag){
            System.out.println("中国将军来到战场!" + count);
            Thread.yield();
            count++;
            /*if(count == 98){
                isFlag = false;
            }*/
        }
    }
}

class TestOne{
    public static void main(String[] args) throws InterruptedException {
        int count = 10;
        ThreadMgSoldier mgSoldier = new ThreadMgSoldier();
        ThreadZhSoldier soldier = new ThreadZhSoldier();
        ThreadMgGeneral general = new ThreadMgGeneral();
        ThreadZhGeneral zhGeneral = new ThreadZhGeneral();
        /*mgSoldier.start();
        soldier.start();*/
        general.start();
        zhGeneral.start();
       /* general.run();
        general.join();
        zhGeneral.run();*/
            
    }
}



//线程睡眠   和 共享对象
class SleepTest{
    public static void main(String[] args) {
        // 创建共享对象  
        Service service = new Service();  
        // 创建线程  
        SleepThread t1 = new SleepThread("t1", service);  
        SleepThread t2 = new SleepThread("t2", service);  
        // 启动线程  
        t1.start();  
        t2.start();  
    }
}



class SleepThread extends Thread{
     private Service service;  
        public SleepThread(String name, Service service) {  
            super(name);  
            this.service = service;  
        }  
        @Override
        public void run() {  
            service.calcs();  
        }  
}

class  Service {
    //同步
    public synchronized void calc(){
        int count = 0;
        System.out.println(Thread.currentThread().getName() + "准备就绪!");
        System.out.println(Thread.currentThread().getName() + "感觉累了准备睡觉!");
        try {
            Thread.sleep(5000); //休眠5秒
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "睡醒继续运算!");
        int ji = (int) (Math.random() * 10);
        count = count - ji;
        System.out.println(Thread.currentThread().getName() + "计算完成!结果为:================"+count);
    }
    
    //不同步
        public  void calcs(){
            int count = 0;
            System.out.println(Thread.currentThread().getName() + "准备就绪!");
            System.out.println(Thread.currentThread().getName() + "感觉累了准备睡觉!");
            try {
                Thread.sleep(5000); //休眠5秒
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "睡醒继续运算!");
            int ji = (int) (Math.random() * 10);
            count = count - ji;
            System.out.println(Thread.currentThread().getName() + "计算完成!结果为:================"+count);
        }
}



class JoinTest{
    public static void main(String[] args) throws InterruptedException {
        JoinThread joinThread1 = new JoinThread("t1");
        JoinThread joinThread2 = new JoinThread("t2");
        joinThread1.start();
        joinThread2.start();
        joinThread2.join();
        joinThread1.join();
        System.out.println("开始");
    }
}


class JoinThread extends Thread {
    public JoinThread(String name){
        //父类的构造方法
          super(name);    
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i = 0 ;i < 10 ;i++){
            System.out.println(getName() + getId() + "执行了" + i + "次");
        }
    }
}
原文地址:https://www.cnblogs.com/xuxuanxubao/p/6369831.html