Java中多线程互斥访问的实现
2016-12-02 15:48
316 查看
1、没有实现线程互斥的函数
1.1 没有实现多线程互斥访问的函数[java] view
plain copy
print?
static 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();
}
}
1.2 测试代码
[java] view
plain copy
print?
public class TestThread {
static final Outputer outputer = new Outputer();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
outputer.output("zhangzhang");
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
outputer.output("lilili");
}
}
}).start();
}
}
1.3 测试结果
出现不正常的输出情况,如下:
2、多线程互斥访问函数的实现一
2.1 实现多线程互斥访问的函数 (在需要加上互斥访问的代码快上,加上synchronized关键字)[java] view
plain copy
print?
static class Outputer {
private String xx = "";
public void output(String name) {
int len = name.length();
synchronized (xx) {
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
在测试代码里面,只new了一个Outputer对象,在两个Runable接口里面调用。这样才能保证用的字符串Sting的xxx是唯一的.
2.2 测试结果
正常
3、多线程互斥访问函数的实现二
3.1 实现多线程互斥访问的函数(在需要加上互斥访问的代码快上,加上synchronized关键字,并使用this关键字)[java] view
plain copy
print?
static 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();
}
}
}
因为每个类的方法里面,本身就有一个this对象,所以不需要再使用一个新的字符串.直接使用this对象就好了。
3.2 测试结果
正常
4、多线程互斥访问函数的实现三
4.1 实现多线程互斥访问的函数(在需要加上互斥访问的代码快上,加上synchronized关键字,并使用类的字节码类)[java] view
plain copy
print?
static class Outputer {
public void output(String name) {
int len = name.length();
synchronized (Outputer.class) {
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
4.2 测试结果
正常
5、多线程互斥访问函数的实现四
5.1 实现多线程互斥访问的函数(在需要加上互斥访问的代码快上,在对应的函数上加上synchronized关键字)[java] view
plain copy
print?
static class Outputer {
public synchronized void output(String name) {
int len = name.length();
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
}
}
5.2 测试结果
正常
6、多线程互斥访问函数的实现五
6.1 实现多线程互斥访问的函数(使用Lock)static class Outputer { ReentrantLock lock = new ReentrantLock(); public void output(String name) { int len = name.length(); try { // 上锁 lock.lock(); for (int i = 0; i < len; i++) { System.out.print(name.charAt(i)); } System.out.println(); } catch (Exception e) { } finally { // 释放锁 lock.unlock(); } } }
6.2 测试结果
正常
相关文章推荐
- java中的多线程包--Semaphore 实现 互斥 与 连接池
- 用WaitForSingleObject()函数实现简单的多线程互斥访问
- Java多线程实现及同步互斥通讯详解
- 多线程实现互斥访问对象的方法
- Bakery Algorithm的c#实现用于多线程互斥访问临界资源
- java多线程资源互斥访问
- 在华为实现的java访问https,多线程,写日志
- java多线程互斥访问小例子
- 彻底明白Java的多线程-实现多线程及线程的同步
- Java多线程-一个简单的线程,实现挂起和恢复的功能
- 实现 Java 多线程并发控制框架
- java通过代理服务器实现对FTP和HTTP的访问
- 在Java中实现多线程
- Java 实现连接sql server 2000(JDBC数据库访问例子)
- Java 实现连接sql server 2000(JDBC数据库访问例子)
- java客户端访问.net实现的WebService
- 当JNI遇到多线程--java对象如何被C++中的多个线程访问?
- Java 实现连接sql server 2000(JDBC数据库访问例子)
- VC++中数据访问互斥的简单实现方法
- Windows XP SP2如何实现多线程访问