《Java多线程编程核心技术》读后感(六)
多线程的死锁
package Second; public class DealThread implements Runnable { public String username;
public Object lock1 = new Object();
public Object lock2 = new Object(); public void setFlag(String username) {
this.username = username;
} @Override
public void run() {
if (username.equals("a")) {
synchronized (lock1) {
try {
System.out.println("username = " + username);
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("按lock1->lock2代码顺序执行了");
}
}
}
if (username.equals("b")) {
synchronized (lock2) {
try {
System.out.println("username = " + username);
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("按lock2->lock1代码顺序执行了");
}
}
}
} }
只要互相等待对方释放锁就有可能出现死锁
内置类与静态内置类
package Second; public class PublicClass { private String username;
private String password; class PrivateClass {
private String age;
private String address; public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public void printPublicProperty() {
System.out.println(username + " " + password);
}
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
package Second; import Second.PublicClass.PrivateClass; public class Run { public static void main(String[] args) { PublicClass publicClass = new PublicClass();
publicClass.setUsername("usernameValue");
publicClass.setPassword("passwordValue"); System.out.println(publicClass.getUsername() + " "
+ publicClass.getPassword()); PrivateClass privateClass = publicClass.new PrivateClass();
privateClass.setAge("ageValue");
privateClass.setAddress("addressValue"); System.out.println(privateClass.getAge() + " "
+ privateClass.getAddress()); } }
package Second; public class PublicClass { static private String username;
static private String password; static class PrivateClass {
private String age;
private String address; public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public void printPublicProperty() {
System.out.println(username + " " + password);
}
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
package Second; import Second.PublicClass.PrivateClass; public class Run { public static void main(String[] args) { PublicClass publicClass = new PublicClass();
publicClass.setUsername("usernameValue");
publicClass.setPassword("passwordValue"); System.out.println(publicClass.getUsername() + " "
+ publicClass.getPassword()); PrivateClass privateClass = new PrivateClass();
privateClass.setAge("ageValue");
privateClass.setAddress("addressValue"); System.out.println(privateClass.getAge() + " "
+ privateClass.getAddress()); } }
内置类与同步:实验1
本实验案例是在内置类中有两个同步方法,但使用的却是不同的锁,打印的结果也是异步的。
package Second; public class OutClass { static class Inner {
public void method1() {
synchronized ("其他的鎖") {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + " i="
+ i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
} public synchronized void method2() {
for (int i = 11; i <= 20; i++) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
}
}
package Second; import Second.OutClass.Inner; public class Run {
public static void main(String[] args) { final Inner inner = new Inner(); Thread t1 = new Thread(new Runnable() {
public void run() {
inner.method1();
}
}, "A"); Thread t2 = new Thread(new Runnable() {
public void run() {
inner.method2();
}
}, "B"); t1.start();
t2.start(); }
}
由于持有不同的“”对象监视器“”,所以打印结果就是乱序的
内置类与同步:实验2
锁对象的改变
package Second; public class MyService {
private String lock = "123"; public void testMethod() {
try {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + " begin "
+ System.currentTimeMillis());
lock = "456";
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " end "
+ System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
package Second; public class ThreadA extends Thread { private MyService service; public ThreadA(MyService service) {
super();
this.service = service;
} @Override
public void run() {
service.testMethod();
}
}
package Second; public class ThreadB extends Thread { private MyService service; public ThreadB(MyService service) {
super();
this.service = service;
} @Override
public void run() {
service.testMethod();
}
}
package Second; public class Run1 { public static void main(String[] args) throws InterruptedException { MyService service = new MyService(); ThreadA a = new ThreadA(service);
a.setName("A"); ThreadB b = new ThreadB(service);
b.setName("B"); a.start();
Thread.sleep(50);
b.start();
}
}
因为50毫秒过后线程B获得的锁时“”456“”
package Second; public class Run2 { public static void main(String[] args) throws InterruptedException { MyService service = new MyService(); ThreadA a = new ThreadA(service);
a.setName("A"); ThreadB b = new ThreadB(service);
b.setName("B"); a.start();
b.start();
}
}
线程AB持有的锁都是“”123“”,虽然将锁改成了“”456“”,但结果还是同步的,因为A和B共同争抢的锁时“”123“”
package Second; public class Userinfo {
private String username;
private String password; public Userinfo() {
super();
} public Userinfo(String username, String password) {
super();
this.username = username;
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
package Second; public class Service { public void serviceMethodA(Userinfo userinfo) {
synchronized (userinfo) {
try {
System.out.println(Thread.currentThread().getName());
userinfo.setUsername("abcabcabc");
Thread.sleep(3000);
System.out.println("end! time=" + System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package Second; public class ThreadA extends Thread { private Service service;
private Userinfo userinfo; public ThreadA(Service service,
Userinfo userinfo) {
super();
this.service = service;
this.userinfo = userinfo;
} @Override
public void run() {
service.serviceMethodA(userinfo);
} }
package Second; public class ThreadB extends Thread { private Service service;
private Userinfo userinfo; public ThreadB(Service service,
Userinfo userinfo) {
super();
this.service = service;
this.userinfo = userinfo;
} @Override
public void run() {
service.serviceMethodA(userinfo);
} }
package Second; public class Run { public static void main(String[] args) { try {
Service service = new Service();
Userinfo userinfo = new Userinfo(); ThreadA a = new ThreadA(service, userinfo);
a.setName("a");
a.start();
Thread.sleep(50);
ThreadB b = new ThreadB(service, userinfo);
b.setName("b");
b.start(); } catch (InterruptedException e) {
e.printStackTrace();
} }
}
上述实验表明:只要对象不变,即使对象的属性被改变,运行的结果还是同步。
《Java多线程编程核心技术》读后感(六)的更多相关文章
- java多线程编程核心技术——第六章总结
目录 1.0立即加载/"饿汉式" 2.0延迟加载/"懒汉式" 3.0使用静态内置类实现单例模式 4.0序列化与反序列化的单例模式实现 5.0使用static代码 ...
- Java多线程编程核心技术(三)多线程通信
线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体.线程间的通信就是成为整体的必用方案之一,可以说,使线程间进行通信后,系统之间的交互性会更强大,在大大提高CPU利用率的同时 ...
- Java多线程编程核心技术(二)对象及变量的并发访问
本文主要介绍Java多线程中的同步,也就是如何在Java语言中写出线程安全的程序,如何在Java语言中解决非线程安全的相关问题.阅读本文应该着重掌握如下技术点: synchronized对象监视器为O ...
- Java多线程编程核心技术(一)Java多线程技能
1.进程和线程 一个程序就是一个进程,而一个程序中的多个任务则被称为线程. 进程是表示资源分配的基本单位,线程是进程中执行运算的最小单位,亦是调度运行的基本单位. 举个例子: 打开你的计算机上的任务管 ...
- Java多线程编程核心技术---学习分享
继承Thread类实现多线程 public class MyThread extends Thread { @Override public void run() { super.run(); Sys ...
- Java多线程编程核心技术---对象及变量的并发访问(二)
数据类型String的常量池特性 在JVM中具有String常量池缓存的功能. public class Service { public static void print(String str){ ...
- Java多线程编程核心技术
Java多线程编程核心技术 这本书有利于对Java多线程API的理解,但不容易从中总结规律. JDK文档 1. Thread类 部分源码: public class Thread implements ...
- 《Java多线程编程核心技术》推荐
写这篇博客主要是给猿友们推荐一本书<Java多线程编程核心技术>. 之所以要推荐它,主要因为这本书写得十分通俗易懂,以实例贯穿整本书,使得原本抽象的概念,理解起来不再抽象. 只要你有一点点 ...
- 《java多线程编程核心技术》(一)使用多线程
了解多线程 进程和多线程的概念和线程的优点: 提及多线程技术,不得不提及"进程"这个概念.百度百科对"进程"的解释如下: 进程(Process)是计算机中的程序 ...
- 《Java 多线程编程核心技术》- 笔记
作为业务开发人员,能够在工作中用到的技术其实不多.虽然平时老是说什么,多线程,并发,注入,攻击!但是在实际工作中,这些东西不见得用得上.因为,我们用的框架已经把这些事做掉了. 比如web开发,外面有大 ...
随机推荐
- hive job oom问题
错误信息例如以下:Container [pid=26845,containerID=container_1419056923480_0212_02_000001] is running beyond ...
- mongodb 集群部署--分片服务器搭建
部署分片服务器 1.分片 为了突破单点数据库服务器的I/O能力限制,对数据库存储进行水平扩展,严格地说,每一个服务器或者实例或者复制集就是一个分片. 2.优势 提供类似现行增·长架构 提高数据可用性 ...
- EasyPlayerPro(Windows)流媒体播放器开发之ffmpeg log输出报错
EasyPlayerPro主要基于ffmpeg进行开发,在EasyPlayerPro开发过程中,曾遇到一个相对比较棘手的问题,该问题一般在播放不是很标准的流或者网络情况较差,容易出现丢帧的情况特别容易 ...
- python书写日志的重要性?
转自:https://blog.csdn.net/weixin_43063753/article/details/82899395 程序为什么要写日志?#为了能够在程序在运行过程中记录错误,方便维护, ...
- linux rsyncserver文件同步
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zqtsx/article/details/24254651 [root@zqtsx]# rpm -q ...
- win7下搭建nginx+php的开发环境(转)
在win7下用的是IIS做web服务器,但近来因项目需求的原因,需要在服务器遇到404错误的时候自动做转向(不是在客户端的跳转,而是在服务器收到客户端请求去某目录下读取文件返回时,如果发现目录或目录下 ...
- 简单Android代码混淆(转)
代码混淆步骤: 1,project.properties中打开代码混淆功能,加入proguard.config=proguard.cfg 2,编辑proguard.cfg文件,项目没有自动生成时可手工 ...
- dojo 官方翻译 dojo/string 版本1.10
官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/string.html#dojo-string require(["dojo/st ...
- 3D立方体旋转动画
在线演示 本地下载
- git format-patch的使用【转】
本文转载自:http://blog.chinaunix.net/uid-28621021-id-3487102.html git format-patch的使用 1.在dev1分支上,打出所有de ...