JAVA学习笔记(四十一)-多线程与线程组
来源:互联网 发布:人工智能程序的维护 编辑:程序博客网 时间:2024/04/30 11:05
线程组ThreadGroup
/* * 线程组ThreadGroup * * 结论: * 如果在设置线程组优先级之前设置线程优先级,则线程优先级不受线程组优先级限制 * 如果在设置线程组优先级之后设置线程优先级,则线程优先级不能超过线程组优先级 * * 线程的优先级,默认与启动它的父线程相同,但受到所有线程组的限制 */public class Test02 { public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + "线程,所属线程组:" + Thread.currentThread().getThreadGroup()); System.out.println(Thread.currentThread()); Thread.currentThread().setPriority(8); Thread th=new Thread("mythread"); System.out.println(th.getPriority()); // 定义一个线程组tg ThreadGroup tg = new ThreadGroup("wbs14061线程组"); System.out.println("tg线程组的父线程组:"+tg.getParent()); //创建线程一,属于tg线程组 Thread th1=new Thread(tg, new MyThread2(), "first"); Thread th2=new Thread(tg, new MyThread2(), "second"); th1.setPriority(8); tg.setMaxPriority(4); th2.setPriority(9); Thread th3=new Thread(tg, new MyThread2(), "third"); System.out.println("tg线程组的信息:"+tg); th1.start(); th2.start(); th3.start(); //tg.stop(); }}class MyThread2 implements Runnable { int num = 1; @Override public void run() { while (num <= 100) { System.out.println(Thread.currentThread().getName() + "," + Thread.currentThread().getPriority() + "****" + num++); } }}
多线程访问共享数据
/* * 多线程访问共享数据 * * 多线程操作共享数据可能会出现问题,即线程安全问题 * 原因:当多个线程操作共享数据时,一个线程只执行了部分语句,还没执行完,此时CPU时间片切换了,另一个线程参与进来,导致共享数据的错误 * 解决:同步机制synchronized * * 同步的前提: * 1.必须要有两个或者两个以上的线程 * 2.必须是多个线程使用同一个锁 * * 保护范围: * 1.只将需要的代码添加到synchronized块中 * 2.不要将run()方法中所有的代码都添加到synchronized块中,否则相当于单线程 * * 线程同步的优缺点: * 优点:解决了线程安全 * 缺点:由于多个线程需要进行锁的判断,消耗资源,导致效率降低 */public class Test03 { public static void main(String[] args) { Ticket ticket = new Ticket(); Thread th1 = new Thread(ticket, "线程1"); Thread th2 = new Thread(ticket, "线程2"); Thread th3 = new Thread(ticket, "线程3"); th1.start(); th2.start(); th3.start(); }}class Ticket implements Runnable { private int num = 100; // 总共100张票,共享此数据 Object obj = new Object(); @Override public void run() { while (true) { // 关键代码块,保护共享数据的安全 synchronized (obj) { if (num > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "售出车票:" + num--); } } } }}
synchronized 方法
/* * 使用synchronized修改方法,称为同步方法(同步函数),线程安全的 * * 同步函数使用的锁是当前对象,即this锁 * 静态同步函数使用的锁是字节码文件对象 */public class Test04 { public static void main(String[] args) { Account account=new Account(); Thread th1=new Thread(account, "你爸"); Thread th2=new Thread(account, "你妈"); th1.start(); th2.start(); }}/* * 银行账户类 */class Bank { private double balance;// 余额 // 查看余额 public void getBalance() { System.out.println("当前余额:" + balance); } // 存钱 public void setBalance(double money) { balance = balance + money; }}/* * 两个人同时往一个账户里打钱,每次打300元,总共打3次,合计900元 */class Account implements Runnable { private static Bank bank = new Bank(); static int i = 1; @Override public void run() { while (true) { saveMoney(); } } //同步函数,同一时间只能有一个线程执行此函数 public static synchronized void saveMoney(){ if (i <= 3) { try { Thread.sleep(10);//休眠10ms,打钱次数会异常 } catch (InterruptedException e) { e.printStackTrace(); } bank.setBalance(300); System.out.println(Thread.currentThread().getName() + "存了300元,第" + (i++) + "次"); bank.getBalance();// 打印当前余额 } else { System.exit(0); } }}
0 0
- JAVA学习笔记(四十一)-多线程与线程组
- java 多线程学习笔记4-线程组 线程池 适配器
- Java进阶(四十一)多线程讲解
- JAVA多线程学习笔记<1>什么是线程
- Java多线程学习笔记—线程停止
- java 多线程学习笔记之 线程中断
- java 多线程学习笔记之 线程同步
- JAVA多线程学习笔记<3> 停止线程
- Java 多线程 学习笔记 线程的停止
- JAVA多线程学习笔记<2>线程的生命周期与线程栈
- JAVA多线程学习笔记<3>线程传入参数与终止线程方法
- java 从零开始,学习笔记之基础入门<Struts2_Spring_整合>(四十一)
- (48)Java学习笔记——多线程 / 线程间通信 / 线程组 / 线程池 /
- Java —— 多线程笔记 三、线程通信 与 线程组、线程异常
- java 多线程学习笔记之 线程实现(线程阻塞)
- java多线程学习之创建线程与线程间通信
- Java线程与多线程
- JAVA线程与多线程
- 012:列表:一个打了激素的数组3
- 动态页面JSP开发
- 通过Proxy访问其它Cas应用
- jsp之重定向和转发理解
- exe ->dll
- JAVA学习笔记(四十一)-多线程与线程组
- memcached并发CAS模式
- mac下抓包需要打开权限
- 阿里双十一数据库技术
- android 显示CPU使用情况
- JAVA学习笔记(四十二)-生产者消费者模型
- HBase的rowkey设计
- JavaScript中事件回顾
- BZOJ2502【上下界最小流】