传统线程互斥技术Synchronized01
来源:互联网 发布:淘宝评价完了怎么截图 编辑:程序博客网 时间:2024/05/20 14:20
package cn.itcast.heima2;
public class ThaditionalThreadSynchronized {
public static void main(String[] args) {
new ThaditionalThreadSynchronized().init();
}
private void init(){
final OutPuter outputer = new OutPuter();
//在静态方法中不能new内部内的实例对象
new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputer.outPut("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputer.outPut2("lihongming");
}
}
}).start();
}
class OutPuter{
public void outPut(String name){
int len=name.length();
synchronized(this){
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();
}
}
}
public class ThaditionalThreadSynchronized {
public static void main(String[] args) {
new ThaditionalThreadSynchronized().init();
}
private void init(){
final OutPuter outputer = new OutPuter();
//在静态方法中不能new内部内的实例对象
new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputer.outPut("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputer.outPut2("lihongming");
}
}
}).start();
}
class OutPuter{
public void outPut(String name){
int len=name.length();
synchronized(this){
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();
}
}
}
0 0
- 传统线程互斥技术Synchronized01
- 传统线程互斥技术
- 传统线程互斥技术
- 传统线程互斥技术 ------ 实例
- 传统线程互斥技术----实现
- 传统线程互斥技术(三)
- 3-传统线程互斥技术
- 传统线程互斥技术Synchronized 02
- 传统线程互斥技术(传统的sychronized)
- 张孝祥java多线程视频笔记----传统线程互斥技术
- 多线程03_张孝祥-传统线程互斥技术
- Java—传统线程互斥技术synchronized
- 【张孝祥并发课程笔记】02:传统线程互斥技术
- Java并发库(三):传统线程互斥技术
- 【java并发】传统线程互斥技术—synchronized
- 多线程总结第三篇之传统线程互斥技术
- Java传统线程中的的互斥技术
- 传统线程的互斥
- (六)swift 在Main.storyboard中的快捷键
- Android开发:常见布局、Logcat和储存方式
- FZU Fire Game
- c#之再战委托事件多线程2_委托和事件结合降低模块低耦合
- Java创建柱状图及饼状图
- 传统线程互斥技术Synchronized01
- Scala Spark 连接 HBase ( IDEA) 调试Spark Standalone
- 1022. Digital Library (30) STL 中 vector,string,map的运用
- iOS开发的52个要点纪要之八
- [leetcode] 97. Interleaving String 解题报告
- LeetCode(279)Perfect Squares
- 在iOS中使用ZXing库
- Eclipse黑色主题,设置字体大小问题
- 使用Timer类的动画