多线程的死锁

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多线程编程核心技术》读后感(六)的更多相关文章

  1. java多线程编程核心技术——第六章总结

    目录 1.0立即加载/"饿汉式" 2.0延迟加载/"懒汉式" 3.0使用静态内置类实现单例模式 4.0序列化与反序列化的单例模式实现 5.0使用static代码 ...

  2. Java多线程编程核心技术(三)多线程通信

    线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体.线程间的通信就是成为整体的必用方案之一,可以说,使线程间进行通信后,系统之间的交互性会更强大,在大大提高CPU利用率的同时 ...

  3. Java多线程编程核心技术(二)对象及变量的并发访问

    本文主要介绍Java多线程中的同步,也就是如何在Java语言中写出线程安全的程序,如何在Java语言中解决非线程安全的相关问题.阅读本文应该着重掌握如下技术点: synchronized对象监视器为O ...

  4. Java多线程编程核心技术(一)Java多线程技能

    1.进程和线程 一个程序就是一个进程,而一个程序中的多个任务则被称为线程. 进程是表示资源分配的基本单位,线程是进程中执行运算的最小单位,亦是调度运行的基本单位. 举个例子: 打开你的计算机上的任务管 ...

  5. Java多线程编程核心技术---学习分享

    继承Thread类实现多线程 public class MyThread extends Thread { @Override public void run() { super.run(); Sys ...

  6. Java多线程编程核心技术---对象及变量的并发访问(二)

    数据类型String的常量池特性 在JVM中具有String常量池缓存的功能. public class Service { public static void print(String str){ ...

  7. Java多线程编程核心技术

    Java多线程编程核心技术 这本书有利于对Java多线程API的理解,但不容易从中总结规律. JDK文档 1. Thread类 部分源码: public class Thread implements ...

  8. 《Java多线程编程核心技术》推荐

    写这篇博客主要是给猿友们推荐一本书<Java多线程编程核心技术>. 之所以要推荐它,主要因为这本书写得十分通俗易懂,以实例贯穿整本书,使得原本抽象的概念,理解起来不再抽象. 只要你有一点点 ...

  9. 《java多线程编程核心技术》(一)使用多线程

    了解多线程 进程和多线程的概念和线程的优点: 提及多线程技术,不得不提及"进程"这个概念.百度百科对"进程"的解释如下: 进程(Process)是计算机中的程序 ...

  10. 《Java 多线程编程核心技术》- 笔记

    作为业务开发人员,能够在工作中用到的技术其实不多.虽然平时老是说什么,多线程,并发,注入,攻击!但是在实际工作中,这些东西不见得用得上.因为,我们用的框架已经把这些事做掉了. 比如web开发,外面有大 ...

随机推荐

  1. Python 深入剖析SocketServer模块(一)(V2.7.11)

    一.简介(翻译)  通用socket server 类  该模块尽力从各种不同的方面定义server:  对于socket-based servers:  -- address family:     ...

  2. kindeditor浏览器兼容性问题

    1.kindeditor在IE下出现异常“对象不支持“attachEvent”属性或方法” 通过开发人员工具会发现: 这时问题就很明了,也就是IE11版本不支持“attachEvent”; 解决方案: ...

  3. 使用服务端的临时密钥,不依赖阿里js的putFIle--》阿里oss

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <title> ...

  4. Python爬虫--Urllib库

    Urllib库 Urllib是python内置的HTTP请求库,包括以下模块:urllib.request (请求模块).urllib.error( 异常处理模块).urllib.parse (url ...

  5. 【题解】[P3557 POI2013]GRA-Tower Defense Game

    [题解][P3557 POI2013]GRA-Tower Defense Game 这道题是真的** 根据题目给的\(k\),可以知道,我们随便放塔,只要不全放一起,一定是一种合法的方案. 直接枚举就 ...

  6. mysql一:操作数据库

    创建数据库是指在数据库空间中划出一块空间用来存储相关的数据,表就是存储在对应的数据库里面.首先来看下查看数据库的命令:show databases. 这个是用来查询数据库空间下所有的数据库,其中inf ...

  7. testng ITestListener使用

    ITestListener适用场景 当使用testng执行测试时,我们常会想在某个阶段做一些特别的处理,比如:测试成功结束后,测试失败后,跳过某个脚本后,全部脚本执行完毕后.要想达成这个目标,我们需要 ...

  8. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. Database: key

    super-key: Any key that has more columns than necessary to uniquely identify each row in the table i ...

  10. Algorithm: Sieve of Eratosthenes

    寻找比n小的所有质数的方法. 2是质数, 2*i都是质数,同样3是质数,3*i也都是质数 代码如下 int n; vector<, true); prime[] = prime[] = fals ...