线程学习之--传统线程互斥
2012-10-13 11:49
183 查看
//线程同步
publicstaticvoidmain(String[] args) {
newSync().show();
}
publicvoidshow(){
final Infoinfo=new Info();
//创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname1("wangyong");
}
}
}).start();
//在创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname1("zhangsan");
}
}
}).start();
}
classInfo{
publicvoidoutname1(String name){
intlen=name.length();
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
以上的实例可以看出,线程不同步。
该如何修改呢?
packagedemo;
publicclass Sync{
/**
*@paramargs
*/
//线程同步
publicstaticvoidmain(String[] args) {
newSync().show();
}
publicvoidshow(){
final Infoinfo=new Info();
//创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname1("wangyong");
}
}
}).start();
//在创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname3("zhangsan");
}
}
}).start();
}
staticclassInfo{
//方法一
publicvoidoutname1(String name){
intlen=name.length();
synchronized(Info.class){
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
//方法二
publicsynchronizedvoidoutname2(String name){
intlen=name.length();
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
//方法二
publicstaticsynchronizedvoidoutname3(String
name){
intlen=name.length();
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
publicstaticvoidmain(String[] args) {
newSync().show();
}
publicvoidshow(){
final Infoinfo=new Info();
//创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname1("wangyong");
}
}
}).start();
//在创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname1("zhangsan");
}
}
}).start();
}
classInfo{
publicvoidoutname1(String name){
intlen=name.length();
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
以上的实例可以看出,线程不同步。
该如何修改呢?
packagedemo;
publicclass Sync{
/**
*@paramargs
*/
//线程同步
publicstaticvoidmain(String[] args) {
newSync().show();
}
publicvoidshow(){
final Infoinfo=new Info();
//创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname1("wangyong");
}
}
}).start();
//在创建一个线程
newThread(new Runnable(){
publicvoidrun() {
while(true){
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
info.outname3("zhangsan");
}
}
}).start();
}
staticclassInfo{
//方法一
publicvoidoutname1(String name){
intlen=name.length();
synchronized(Info.class){
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
//方法二
publicsynchronizedvoidoutname2(String name){
intlen=name.length();
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
//方法二
publicstaticsynchronizedvoidoutname3(String
name){
intlen=name.length();
for(inti=0;i<len;i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
相关文章推荐
- 张孝祥[致敬]-多线程学习第01课 传统线程的创建
- 线程回顾第三篇:传统线程互斥与同步通讯
- 张孝祥java.concurrent线程并发学习笔记 - 传统线程回顾
- 多线程学习-传统线程技术创建
- 线程学习之--传统的线程技术
- 线程学习二,ReentrantLock.Lock实现线程互斥
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
- 多线程01_张孝祥-传统线程的3种创建
- [置顶] Java 多线程 学习笔记(二)停止线程的几种方法
- 线程的两种创建及线程池——学习二
- (三)Netty源码学习笔记之boss线程处理流程
- java线程学习-Thread.currentTread().getName()和this.getName()的区别
- Android学习三:线程操作
- 传统线程同步通讯技术
- 【RT-Thread学习笔记 4】线程让出实验
- java学习之路一、工作线程
- C# 初步学习线程
- java基础学习之线程
- 学习笔记-java线程
- Netty IO线程模型学习总结