2个线程在同一个实例的方法中输出
会出现如下结果:
zhangxiaoxiang
zhangxiaoxiang
lihuoming
zhangxiaoxiang
lihuomizhangxiaoxiang
ng
zhangxiaoxiang
lihuoming
zhangxiaoxiang
如下代码可以产生错误的结果:
1、2个线程同时访问,output方法一个线程没有允许完成另一个线程就进来。
package cn.itcast.heima2;
public class TraditionalTreadSynchrorized {
public static void main(String[] args) {
new TraditionalTreadSynchrorized().init();
}
public void init() {
final Outputer output = new Outputer();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("lihuoming");
}
}
}).start();
}
class Outputer {
public void output(String name) {
int len = name.length();
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
正确代码:
使用对象锁机制将输出体进行锁定
synchronized (this) { }
package cn.itcast.heima2;
public class TraditionalTreadSynchrorized {
public static void main(String[] args) {
new TraditionalTreadSynchrorized().init();
}
public void init() {
final Outputer output = new Outputer();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("lihuoming");
}
}
}).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();
}
}
}
}
2、错误代码
虽然两个线程调用的方法不同,可是还是在同一个对象中,当一个线程没有输出完毕的时候第二个线程就打印了
system.out.println();进行换行。
package cn.itcast.heima2;
public class TraditionalTreadSynchrorized {
public static void main(String[] args) {
new TraditionalTreadSynchrorized().init();
}
public void init() {
final Outputer output = new Outputer();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output2("lihuoming");
}
}
}).start();
}
class Outputer {
public void output(String name) {
int len = name.length();
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
public 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 关键字加在方法上默认锁定this 对象 如果在方法中需要 使用synchronized(this){}
package cn.itcast.heima2;
public class TraditionalTreadSynchrorized {
public static void main(String[] args) {
new TraditionalTreadSynchrorized().init();
}
public void init() {
final Outputer output = new Outputer();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output2("lihuoming");
}
}
}).start();
}
static class Outputer {
public void output(String name) {
synchronized (this) {
int len = name.length();
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不是锁定的同一个对象。
package cn.itcast.heima2;
public class TraditionalTreadSynchrorized {
public static void main(String[] args) {
new TraditionalTreadSynchrorized().init();
}
public void init() {
final Outputer output = new Outputer();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Outputer.output3("lihuoming");
}
}
}).start();
}
static class Outputer {
public void output(String name) {
synchronized (this) {
int len = name.length();
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
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();
}
}
}
正确代码如下:
因为需要锁定静态对象 所以要使用Outputer.class
package cn.itcast.heima2;
public class TraditionalTreadSynchrorized {
public static void main(String[] args) {
new TraditionalTreadSynchrorized().init();
}
public void init() {
final Outputer output = new Outputer();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
output.output("zhangxiaoxiang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Outputer.output3("lihuoming");
}
}
}).start();
}
static class Outputer {
public void output(String name) {
synchronized (Outputer.class) {
int len = name.length();
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
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();
}
}
}
分享到:
相关推荐
易语言 线程池 多线程 线程互斥 易语言线程互斥的解决办法
资源名称:Java多线程与并发库高级应用视频教程22集资源目录:【】01传统线程技术回顾【】02传统定时器技术回顾【】03传统线程互斥技术【】04传统线程同步通信技术【】04传统线程同步通信技术_分割纪录【】05线程...
g: 利用互斥量来解决线程同步互斥问题 h: problem1 生产者消费者问题 (1生产者 1消费者 1缓冲区) problem1 more 生产者消费者问题 (1生产者 2消费者 4缓冲区) problem2 读者与写着问题 I: 信号量 semaphore ...
这个多线程例子。C#的。主要讲述线程互斥的问题 。只是个简单的例子。
C 创建线程互斥对象的实例源码下载,声明线程函数,创建线程,程序睡眠,释放互斥对象,设置事件对象为无信号状态,生成控制台程序,仅供参考。
C#多线程互斥实例 多线程获取同一变量(不重复)。是一个很好的学习例子
4个线程互斥类,代码例子,完整的工程,详细说明了4中互斥对象的作用,对学习多线程编程的朋友很有用处............
C#.NET多线程实例6个(包括多线程基本使用,多线程互斥等全部多线程使用实例)
C# 多线程互斥 两个线程交替工作 C#多线程互斥,两个线程交替工作,如上图所示,挺有意思的。
线程互斥测试2!
操作系统实验(三)线程的互斥 操作系统实验(三)线程的互斥
多线程中互斥量的使用。 涉及到得知识有:线程的创建、互斥量的创建和使用,线程等待等。
线程的互斥和协作,是线程学习的一个重要关节,祝大家能学好
进程,线程同步互斥的控制方法,详细介绍,个人总结,面试常问
线程 互斥.rar
C++ 创建线程互斥对象的实例源码下载,声明线程函数,创建线程,程序睡眠,释放互斥对象,设置事件对象为无信号状态,生成控制台程序,仅供参考。
vc++中使用临界区CriticalSection的例子.zip
MFC线程互斥!
四种进程或线程同步互斥的控制方法,用c++实现的,很不错的东西,大家共享一下