Java自学笔记04(线程01)

Thread中方法的测试

package ThreadMethodTest;

/**
 * @author zzb
 * @version V1.0
 * @creat 2022 0406 20:31
 * <p>
 * Thread中方法的测试
 */

class HelloThread extends java.lang.Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {

          /*      try {
                    sleep(10);//让当前线程阻塞(睡眠)指定的milltime毫秒,在指定的mill time中,当前线程是阻塞状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/

                System.out.println(Thread.currentThread().getName() + ":" + i + ":" + Thread.currentThread().getPriority());
            }
            if (i % 20 == 0) {
                yield();//释放当前cpu的执行权,
            }
        }
    }
}

public class ThreadMethodTest {
    public static void main(String[] args) {
        HelloThread h1 = new HelloThread();
        //设置分线程的优先级
        h1.setPriority(Thread.MAX_PRIORITY);//设置线程的优先级,概率上有可能会被优先执行,不一定会一定会被执行:高优先级抢占低优先级cpu的执行权
        h1.setName("线程一");//设置线程的名字
        h1.start();

        Thread.currentThread().setName("主线程");
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i + ":" + Thread.currentThread().getPriority());
            }
            if (i == 20) {
                try {
                    h1.join();//在线程A中调用线程B的join()方法,此线程A进入阻塞状态,直到线程B完全执行完以后,线程A才结束阻塞状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(h1.isAlive());//判断该线程是否还存活
    }
}

多线程的创建,方式一:继承于Thread类的方式

/**
 * 多线程的创建,方式一:继承于Thread类的方式
 *
 * @author zzb
 * @version V1.0
 * @creat 2022 0406 11:23
 * <p>
 * 遍历100以内的所有的偶数
 */

class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
            }
        }
    }
}

public class ThreadTest {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        t1.start();
        MyThread t2 = new MyThread();
        t2.start();
        System.out.println("hello");
    }
}

创建多线程的方式二: 实现Runnable接口

/**
 * 创建多线程的方式二: 实现Runnable接口
 *
 * @author zzb
 * @version V1.0
 * @creat 2022 0406 21:27
 * <p>
 */

class Mthread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() +"\t"+ i);
            }
        }
    }
}

public class ThreadTest01 {
    public static void main(String[] args) {
        Mthread mThread = new Mthread();
        Thread t1 = new Thread(mThread);
        t1.start();
        Thread t2 = new Thread(mThread);
        t2.start();
    }
}

卖票窗口的多线程实现

/**
 * @author zzb
 * @version V1.0
 * @creat 2022 0406 21:16
 */

class Window extends Thread {

    private static int ticket = 100;

    @Override
    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(getName() + ":卖票,票号为:" + ticket);
                ticket--;
            } else
                break;
        }
    }
}

public class WindowTest {
    public static void main(String[] args) {
        Window t1 = new Window();
        Window t2 = new Window();
        Window t3 = new Window();

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}

使用Runnable接口的方式实现–>通过同步机制实现线程安全问题

/**
 * @author zzb
 * @version V1.0
 * @creat 2022 0406 21:37
 * <p>
 * 使用Runnable接口的方式实现-->通过同步机制实现线程安全问题
 * 方式一: 同步代码块
 * 方式二: 同步方法
 */

class Window1 implements Runnable {

    private int ticket = 100;
    //Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (this) {//同步代码块(同步监视器==>锁)  this 为window1的w对象
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);

                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}

public class WindowTest1 {
    public static void main(String[] args) {
        Window1 w = new Window1();//只new了一个对象,所以公用一个ticket

        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}

同步代码块中的同步监视器:

非静态的---->(this)类中的对象
静态的 ------>当前类本身


版权声明:本文为ZZBvos原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。