muyou -- 线程
来源:互联网 发布:淘宝nike鸳鸯球鞋 编辑:程序博客网 时间:2024/06/03 21:25
package webDemo;public class SyncTest {static class PrintMachine{// 线程不安全的private StringBuilder sb = new StringBuilder();public synchronized void print(String content) {sb.append(Thread.currentThread().getName()).append(":").append(content).append("\r\n"); }public synchronized void show() {System.out.println(sb.toString());}} public static void main(String[] args) {PrintMachine mc = new PrintMachine();// 用户1 Runnable run1 = new Runnable() {@Overridepublic void run() {mc.print("user1");}};// 用户2Runnable run2 = new Runnable() {@Overridepublic void run() {mc.print("user2");}};new Thread(run1,"线程1的名字").start();new Thread(run2,"线程2的名字").start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}mc.show();}}
阅读全文
0 0
- muyou -- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 使用DHT11制作一个Arduino温度数据记录仪
- GoogleCamera代码流程分析之相机切换
- json在项目中的应用大总结
- vue2.0购物车和地址选配实例,附演示地址
- poj2185 Milking Grid(两次KMP)
- muyou -- 线程
- runtime error program:c:\windows\explorer.exe (安装win8安全更新补丁后出现) 为什么?
- linux中的SUID和SBIT 权限
- 单调队列——Feel Good(良好的感觉)
- 快速幂与快速矩阵幂(以大数下的斐波那契数列为例)
- centos7.1 QT5编译出现:cannot find -lGL 和 collect2:error:ld returned 1 exit status 错误
- 什么情况下会导致内存泄露
- 高级特性
- hdu 2111 贪心