java 给指定线程加锁_java多线程加锁是对谁加锁?

1.java多线程加锁是对谁加锁?

答:当然是对共享资源加锁啊,对谁进行访问修改,就对象进行加锁。以便使多线程按序访问此共享对象

比如:

在具体的Java代码中需要完成一下两个操作:

把竞争访问的资源类Foo变量x标识为private;

同步哪些修改变量的代码,使用synchronized关键字同步方法或代码。

48304ba5e6f9fe08f3fa1abda7d326ab.png

package cn.thread;

public class Foo2 {

private int x = 100;

public int getX() {

return x;

}

//同步方法

public synchronized int fix(int y) {

x = x - y;

System.out.println("线程"+Thread.currentThread().getName() + "运行结束,减少“" + y

+ "”,当前值为:" + x);

return x;

}

// //同步代码块

// public int fix(int y) {

// synchronized (this) {

// x = x - y;

// System.out.println("线程"+Thread.currentThread().getName() + "运行结束,减少“" + y

// + "”,当前值为:" + x);

// }

//

// return x;

// }

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

package cn.thread;

public class MyRunnable2 {

public static void main(String[] args) {

MyRunnable2 run = new MyRunnable2();

Foo2 foo2=new Foo2();

MyThread t1 = run.new MyThread("线程A", foo2, 10);

MyThread t2 = run.new MyThread("线程B", foo2, -2);

MyThread t3 = run.new MyThread("线程C", foo2, -3);

MyThread t4 = run.new MyThread("线程D", foo2, 5);

t1.start();

t2.start();

t3.start();

t4.start();

}

class MyThread extends Thread {

private Foo2 foo2;

/**当前值*/

private int y = 0;

MyThread(String name, Foo2 foo2, int y) {

super(name);

this.foo2 = foo2;

this.y = y;

}

public void run() {

foo2.fix(y);

}

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

线程线程A运行结束,减少“10”,当前值为:90

线程线程C运行结束,减少“-3”,当前值为:93

线程线程B运行结束,减少“-2”,当前值为:95

线程线程D运行结束,减少“5”,当前值为:90

比如:

对于同步,除了同步方法外,还可以使用同步代码块,有时候同步代码块会带来比同步方法更好的效果。

追其同步的根本的目的,是控制竞争资源的正确的访问,因此只要在访问竞争资源的时候保证同一时刻只能一个线程访问即可,因此Java引入了同步代码快的策略,以提高性能。

在上个例子的基础上,对oper方法做了改动,由同步方法改为同步代码块模式,程序的执行逻辑并没有问题。

48304ba5e6f9fe08f3fa1abda7d326ab.png

package cn.thread;

/**

* 线程同步方法

*

* @author 林计钦

* @version 1.0 2013-7-24 上午10:12:47

*/

public class ThreadSynchronizedCode {

public static void main(String[] args) {

ThreadSynchronizedCode t = new ThreadSynchronizedCode();

User u = t.new User("张三", 100);

MyThread t1 = t.new MyThread("线程A", u, 20);

MyThread t2 = t.new MyThread("线程B", u, -60);

MyThread t3 = t.new MyThread("线程C", u, -80);

MyThread t4 = t.new MyThread("线程D", u, -30);

MyThread t5 = t.new MyThread("线程E", u, 32);

MyThread t6 = t.new MyThread("线程F", u, 21);

t1.start();

t2.start();

t3.start();

t4.start();

t5.start();

t6.start();

}

class MyThread extends Thread {

private User u;

/**存款金额*/

private int y = 0;

MyThread(String name, User u, int y) {

super(name);

this.u = u;

this.y = y;

}

public void run() {

u.oper(y);

}

}

class User {

/** 账号 */

private String code;

/** 余额 */

private int cash;

User(String code, int cash) {

this.code = code;

this.cash = cash;

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

/**

* 存款

*

* @param x 欲存款金额

*

*/

public void oper(int x) {

try {

Thread.sleep(10L);

synchronized (this) {

this.cash += x;

System.out.println("线程" + Thread.currentThread().getName() + "运行结束,增加“" + x

+ "”,当前用户账户余额为:" + cash);

}

Thread.sleep(10L);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

@Override

public String toString() {

return "User{" + "code='" + code + '\'' + ", cash=" + cash + '}';

}

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

线程线程B运行结束,增加“-60”,当前用户账户余额为:40

线程线程A运行结束,增加“20”,当前用户账户余额为:60

线程线程C运行结束,增加“-80”,当前用户账户余额为:-20

线程线程D运行结束,增加“-30”,当前用户账户余额为:-50

线程线程F运行结束,增加“21”,当前用户账户余额为:-29

线程线程E运行结束,增加“32”,当前用户账户余额为:3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值