JavaSE(19)(两种开启多线程方式)

TestFor类:
package zz.itheima.multithread;

public class TestFor {

    public static void main(String[] args) {
        // 我们想让两个独立的for循环同时执行,但是按照之前的写法无法实现
        for(int j=0;j<10;j++)
        {
            System.out.println(j);
        }
        //一个for运行完了之后才能继续运行另外一个
        for(int j=0;j<10;j++)
        {
            System.out.println(j);
        }
    }

}
ThreadFor类:
package zz.itheima.multithread;

public class ThreadFor extends Thread{
    //用多线程让两个for同时运行
    @Override
    public void run() {//该方法中是需要同时运行的代码
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+"-"+i);
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {//主线程

        //创建两条生产线
        ThreadFor t1 = new ThreadFor();
        ThreadFor t2 = new ThreadFor();
        //启动生产线
        t1.start();
        t2.start();

        for(int j=0;j<20;j++){
            System.out.println(Thread.currentThread().getName()+"-"+j);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

}
ThreadClassroom类:
package zz.itheima.multithread;
//方式一实现教室同时上课
public class ThreadClassroom extends Thread{

    private static int count = 5;
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"上课"+i+"分钟");
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        count--;
        System.out.println("剩下"+count+"个教室");
    }

    public static void main(String[] args) {
        ThreadClassroom ts1 = new ThreadClassroom();
        ThreadClassroom ts2 = new ThreadClassroom();
        ts1.start();
        ts2.start();
    }

}
ThreadStudent类:
package zz.itheima.multithread;

/** * 测试多线程(学生看书) * * @author Administrator * */
public class ThreadStudent extends Thread {

    private void read() {
        for (int i = 0; i <= 10; i++) {
            System.out.println(Thread.currentThread().getName()+"在看第"+i +"页");
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        read();
    }

    public static void main(String[] args) {
        ThreadStudent s1 = new ThreadStudent();
        ThreadStudent s2 = new ThreadStudent();
        s1.start();
        s2.start();
    }

}
RunnableClassroom类:
package zz.itheima.multithread;


//方式二实现教室同时上课
public class RunnableClassroom implements Runnable {
    private int count = 5;
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"上课"+i+"分钟");
            try {
                Thread.sleep(10);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        count--;
        System.out.println("还剩下"+count+"个教室");

    }

    public static void main(String[] args) {
        RunnableClassroom rc = new RunnableClassroom();
        new Thread(rc).start();
        new Thread(rc).start();
    }

}
RunnableStudent类:
package zz.itheima.multithread;

public class RunnableStudent implements Runnable{

    public void run() {
        for(int j=1;j<=10;j++)
        {
            System.out.println(Thread.currentThread().getName()+"在看第"+j+"页");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        RunnableStudent rs=new RunnableStudent();

        new Thread(rs).start();
        new Thread(rs).start();

    }

}
RunnableFor类:
package zz.itheima.multithread;

public class RunnableFor implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+"-"+i);
        }
    }

    public static void main(String[] args) {
        RunnableFor rf = new RunnableFor();
        Thread t1 = new Thread(rf);
        t1.start();
        Thread t2 = new Thread(rf);
        t2.start();
    }

}
RightClassRoom类:
package zz.itheima.multithread;


//有10个教室可供出租,两个人同时都需要,这里是否存在线程安全问题,请解决。
public class RightClassRoom implements Runnable{
    private int count = 5;
    /** * 静态方法的锁 */
    public static void read(){
        synchronized (RightClassRoom.class) {
        }
    }
    @Override
    public void run() {
        for (int i = 0; i <10; i++) {
            synchronized (this) {//同步代码块
                if (count>0) {
                    System.out.println("编号"+count+"的自习室被出租");
                    count--;
                    System.out.println("剩下"+count+"个");
                    try {
                        Thread.sleep(10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    public static void main(String[] args) {
        RightClassRoom rc = new RightClassRoom();
        new Thread(rc).start();
        new Thread(rc).start();
    }



}
SaleWindow类:
package zz.itheima.multithread;
//两个窗口同时卖火车票
public class SaleWindow implements Runnable{
    private static int num = 10;
    /** * 同步静态方法 */
    public synchronized static void sell(){
        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "卖编号"
                    + num + "火车票");
            num--;
            System.out.println("还剩下" + num + "张票");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            sell();

        }
        /*for (int i = 0; i <= 10; i++) { synchronized (this) { if (num>0) { System.out.println(Thread.currentThread().getName()+"卖编号"+num+"的火车票"); num--; System.out.println("还剩下"+num+"张票"); try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } } } }*/
    }

    public static void main(String[] args) {
        SaleWindow sw = new SaleWindow();
        new Thread(sw).start();
        new Thread(sw).start();
    }
}
七星彩:
package zz.itheima.multithread;

/*请使用多线程实现模拟中国体彩七星彩的开奖程序。 提示:七星彩的整个开奖过程需要同时产生7位中奖号码, 每个中奖号码的范围在0至9之间。*/
import java.util.Random;

public class Work1 extends Thread{
    public void getNum(){
        Random r=new Random();
        System.out.print(r.nextInt(10));
    }

    public void run(){
        getNum();
    }

    public static void main(String[] args) {
        Work1 w1=new Work1();
        Work1 w2=new Work1();
        Work1 w3=new Work1();
        Work1 w4=new Work1();
        Work1 w5=new Work1();
        Work1 w6=new Work1();
        Work1 w7=new Work1();

        w1.start();
        w2.start();
        w3.start();
        w4.start();
        w5.start();
        w6.start();
        w7.start();

    }

}
预定房间:
package zz.itheima.multithread;
/*某大型国际酒店有100个房间,来店住宿必须提前预定。 为了提高预定效率,设有3部电话同时接听顾客来电,这里是否存在线程安全的问题?请解决。*/
public class Work2 implements Runnable {

    private int num=10;

    public void run() {
        for (int i = 0; i < 10; i++) {
            synchronized (this) {
                if (num>0) {
                    System.out.println("编号" + num + "的房间已经被预定了");
                    num--;
                }
            }

        }
    }

    public static void main(String[] args) {
        Work2 w=new Work2();

        new Thread(w).start();
        new Thread(w).start();
        new Thread(w).start();
    }
}

你可能感兴趣的:(java,多线程,se)