java多线程加锁
来源:互联网 发布:数据库substring 编辑:程序博客网 时间:2024/04/30 02:46
package com.zhlk.thread;public class TraditionalThreadSync {/** * 创建日期:2017-3-4下午10:53:49 作者:lvguanghui */public static void main(String[] args) { new TraditionalThreadSync().init();} private void init(){ final Outputer output=new Outputer(); new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } output.output3("lvguanghui"); } } }).start(); new Thread(new Runnable() { @Overridepublic void run() { while(true){ try {Thread.sleep(500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} output.output1("lvjinhan"); } }}).start(); }static class Outputer { public synchronized void output1(String name) {int len = name.length();//在方法内部使用关键字synchronized对代码块实现加锁synchronized (Outputer.class) { for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));} System.out.println(); } } public synchronized void output2(String name) {int len = name.length(); for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));} System.out.println(); } //在方法中使用关键字synchronized实现加锁 public static synchronized void output3(String name) {int len = name.length(); for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));} System.out.println(); }}}
0 0
- Java 多线程加锁
- java 多线程 加锁
- java多线程加锁
- java多线程以及资源加锁
- 多线程加锁
- 多线程加锁
- 多线程加锁
- java多线程编程--抽象方法加锁
- Java多线程——加锁机制
- java 多线程加锁-锁住了什么?
- java加锁
- java多线程、并发系列之 (synchronized)同步与加锁机制
- java多线程学习之通过synchronized加锁解决线程安全问题
- Java多线程实现窗口背景颜色轮换(synchronize加锁)
- 多线程读写文件 加锁
- object-c 多线程 加锁
- object-c 多线程 加锁
- IOS 多线程加锁
- 信息安全技术实验三邮件服务安全实验
- window配置redis(个人记录)
- Linux探秘之用户态与内核态
- java高并发处理
- Spark 安装
- java多线程加锁
- 关于c++中库函数说明
- Cerebro 一个跨平台的桌面快捷使用工具
- Today一只菜鸡的PAT甲级测试(PAT1124, PAT1125, PAT1126, PAT1127)
- Sleeping会话导致阻塞原理(下)
- 最长公共子序列
- EditText 相关
- JavaEE 使用OKhttp和Action进行通信
- 爱和自由