博客
关于我
java基础学习总结——线程(二)
阅读量:357 次
发布时间:2019-03-04

本文共 5084 字,大约阅读时间需要 16 分钟。

?????????????

?????????????????????????setPriority()????????????????NORM_PRIORITY??5???????????t1??????????8??????t2???????????????????

package cn.galc.test;public class TestThread6 {    public static void main(String args[]) {        MyThread4 t4 = new MyThread4();        MyThread5 t5 = new MyThread5();        Thread t1 = new Thread(t4);        Thread t2 = new Thread(t5);        t1.setPriority(Thread.NORM_PRIORITY + 3);        t1.start();        t2.start();        System.out.println("t1????????" + t1.getPriority());    }}class MyThread4 implements Runnable {    public void run() {        for (int i = 0; i <= 1000; i++) {            System.out.println("T1?" + i);        }    }}class MyThread5 implements Runnable {    public void run() {        for (int i = 0; i <= 1000; i++) {            System.out.println("T2?" + i);        }    }}

????????????????????synchronized???????????????????????????????????????????Timer???synchronized??????????????????????????

package cn.galc.test;public class TestSync implements Runnable {    Timer timer = new Timer();    public static void main(String args[]) {        TestSync test = new TestSync();        Thread t1 = new Thread(test);        Thread t2 = new Thread(test);        t1.setName("t1");        t2.setName("t2");        t1.start();        t2.start();    }    public void run() {        timer.add(Thread.currentThread().getName());    }}class Timer {    private static int num = 0;    public void add(String name) {        synchronized (this) {            num++;            try {                Thread.sleep(1);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(name + "????" + num + "???timer???");        }    }}

????????????????????????????????????????????????????????????????o1?o2??????????????????????

package cn.galc.test;public class TestDeadLock implements Runnable {    public int flag = 1;    static Object o1 = new Object(), o2 = new Object();    public void run() {        System.out.println(Thread.currentThread().getName() + "?flag=" + flag);        if (flag == 1) {            synchronized (o1) {                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    e.printStackTrace();                }                synchronized (o2) {                    System.out.println("1");                }            }        }        if (flag == 0) {            synchronized (o2) {                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    e.printStackTrace();                }                synchronized (o1) {                    System.out.println("0");                }            }        }    }    public static void main(String args[]) {        TestDeadLock td1 = new TestDeadLock();        TestDeadLock td2 = new TestDeadLock();        td1.flag = 1;        td2.flag = 0;        Thread t1 = new Thread(td1);        Thread t2 = new Thread(td2);        t1.setName("??td1");        t2.setName("??td2");        t1.start();        t2.start();    }}

??????????????????????????????????????????????

?????????????????????????????????????????????????????????????????????????????synchronized????wait/notify????????????????

package cn.galc.test;public class ProducerConsumer {    public static void main(String args[]) {            SyncStack stack = new SyncStack();            Runnable p = new Producer(stack);            Runnable c = new Consumer(stack);            Thread p1 = new Thread(p);            Thread c1 = new Thread(c);            p1.start();            c1.start();    }}class SyncStack {    private int index = 0;    private char[] data = new char[6];    public synchronized void push(char c) {        if (index == data.length) {            try {                this.wait();            } catch (InterruptedException e) {            }        }        this.notify();        data[index] = c;        index++;    }    public synchronized char pop() {        if (index == 0) {            try {                this.wait();            } catch (InterruptedException e) {            }        }        this.notify();        index--;        return data[index];    }}class Producer implements Runnable {    SyncStack stack;    public Producer(SyncStack s) {        stack = s;    }    public void run() {        for (int i = 0; i < 20; i++) {            char c = (char) (Math.random() * 26 + 'A');            stack.push(c);            System.out.println("produced?" + c);            try {                Thread.sleep((int) (Math.random() * 1000));            } catch (InterruptedException e) {            }        }    }}class Consumer implements Runnable {    SyncStack stack;    public Consumer(SyncStack s) {        stack = s;    }    public void run() {        for (int i = 0; i < 20; i++) {            char c = stack.pop();            System.out.println("???" + c);            try {                Thread.sleep((int) (Math.random() * 1000));            } catch (InterruptedException e) {            }        }    }}

转载地址:http://fckr.baihongyu.com/

你可能感兴趣的文章
Node-RED中使用node-random节点来实现随机数在折线图中显示
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node.js 8 中的 util.promisify的详解
查看>>
Node.js 函数是什么样的?
查看>>
Node.js 历史
查看>>
Node.js 在个推的微服务实践:基于容器的一站式命令行工具链
查看>>
Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
查看>>
node.js 怎么新建一个站点端口
查看>>
Node.js 文件系统的各种用法和常见场景
查看>>
node.js 简易聊天室
查看>>
node.js 配置首页打开页面
查看>>
node.js+react写的一个登录注册 demo测试
查看>>
Node.js中环境变量process.env详解
查看>>
Node.js卸载超详细步骤(附图文讲解)
查看>>
Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
查看>>
Node.js安装及环境配置之Windows篇
查看>>
Node.js安装和入门 - 2行代码让你能够启动一个Server
查看>>
node.js安装方法
查看>>