线程是干活的
所以线程一定是Thread,或者该线程实现Runnable接口
多线程是竞争关系,所以多个线程竞争同一个资源,也就是同一个对象
所以这个竞争对象放到Thread中
即:
// resources是竞争资源
Resources resources = new Resources();
Thread1 thread1 = new Thread1(resources);
Thread2 thread2 = new Thread2(resources);

thread1.start();
thread2.start();

----------------------------------------------------------------------
class Thread1 implements Runnable {
  Resources resources = null;
  Thread1(Resources resources) {
    this.resources = resources;
  }

  public void run() {
    //这个methodA方法时Resources里面的竞争资源方法
    resources.methodA();
  }
}

class Thread2 implements Runnable {
  Resources resources = null;
  Thread2(Resources resources) {
    this.resources = resources;
  }

  public void run() {
    //这个methodA方法时Resources里面的竞争资源方法
    resources.methodA();
  }

}

class Resources {
  private int count = 100;
  //多线程去干活了,它们争着抢着去执行竞争资源里面的方法,所以这个方法区域需要加锁
  public synchronized void methodA() {
    if(count > 0) {
      count--;
     }
  }
}

例子:

package Thread;

public class MultiThread {

    public static void main(String[] args) {
//resources就是竞争资源对象
Resources resources = new Resources();
Runnable1 runnable1 = new Runnable1(resources); for(int i = 0; i <100; i++) {
// 这里是创建多线程去执行任务
//多线程是竞争关系,所以多个线程竞争同一个资源,也就是同一个对象
//所以这个竞争对象放到Thread中
new Thread(runnable1,"Thread"+i).start();
}
} } class Resources {
private int count = 100; //多线程去干活了,它们争着抢着去执行竞争资源里面的方法,所以这个方法区域需要加锁
public synchronized void methodA() {
if(count > 0) {
count--;
}
System.out.println(Thread.currentThread().getName() + " " +"count:"+count);
}
} class Runnable1 implements Runnable {
Resources resources = null;
Runnable1(Resources resources) {
this.resources = resources;
} public void run() {
//这个methodA方法时Resources里面的竞争资源方法
resources.methodA();
} }

.....

多线程可以同时访问同个对象的不同方法吗?

例子:

public class Test66 {
public static void main(String[] args) {
A a = new A();
Thread1 thread1 = new Thread1(a);
Thread2 thread2 = new Thread2(a); thread1.start();
thread2.start();
}
} class A{
public synchronized void method1() throws InterruptedException {
System.out.println("进入method1方法睡5秒");
Thread.sleep(5000);
} public synchronized void method2() throws InterruptedException {
System.out.println("进入method1方法睡2秒");
Thread.sleep(2000);
}
} class Thread1 extends Thread {
A a;
public Thread1(A a) {
this.a = a;
} @Override
public void run() {
try {
a.method1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} class Thread2 extends Thread {
A a;
public Thread2(A a) {
this.a = a;
} @Override
public void run() {
try {
a.method2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

修改一下 :讲method2的 synchronized 去掉

class A{
public synchronized void method1() throws InterruptedException {
System.out.println("进入method1方法睡5秒");
Thread.sleep(5000);
System.out.println("进入method1结束");
} public void method2() throws InterruptedException {
System.out.println("进入method2方法睡2秒");
Thread.sleep(2000);
System.out.println("进入method2结束");
}
}

因此得出结论:同个对象的两个同步方法不能并发执行,也就是一个线程获取了一个对象的锁之后,对应这个对象的其他同步方法也被锁住,其他线程只能等待。若方法没有被synchronized 修饰,则可以多线程并发执行

java 如何编写多线程的代码的更多相关文章

  1. 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁

    什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...

  2. java 22 - 9 多线程之 代码实现的方式2

    多线程的代码实现: 方式2:实现Runnable接口 步骤: A:自定义类MyRunnable实现Runnable接口 B:重写run()方法 C:创建MyRunnable类的对象 D:创建Threa ...

  3. java 22 - 4 多线程的代码实现的方式1

    需求:我们要实现多线程的程序. 如何实现呢? 由于线程是依赖进程而存在的,所以我们应该先创建一个进程出来. 而进程是由系统创建的,所以我们应该去调用系统功能创建一个进程. Java是不能直接调用系统功 ...

  4. Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)

    Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...

  5. java如何编写多线程

    1.如何实现多线程 1.1实现Runnable接口,实现run()方法. public class Main4 implements Runnable { public static void mai ...

  6. 使用java语言编写窗口按钮

    使用java语言编写窗口按钮 代码如下: package Day08; import java.awt.FlowLayout; import javax.swing.JButton;import ja ...

  7. 编写高质量代码改善java程序的151个建议——导航开篇

    2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...

  8. Java编写高质量代码改善程序的151个建议

    第一章  Java开发中通用的方法和准则 建议1:不要在常量和变量中出现易混淆的字母: (i.l.1:o.0等). 建议2:莫让常量蜕变成变量: (代码运行工程中不要改变常量值). 建议3:三元操作符 ...

  9. 编写高质量代码:改善Java程序的151个建议 --[117~128]

    编写高质量代码:改善Java程序的151个建议 --[117~128] Thread 不推荐覆写start方法 先看下Thread源码: public synchronized void start( ...

随机推荐

  1. Gitlab启动、停止、重启(两种启动方式)

    因为Gitlab不是我部署的,是之前总监部署的,服务器突然更新系统了,Git服务器就没有自启··自启··自启······,自己操作启动没有成功,然后网上搜了一下都是这三种启动关闭重启的方式,可是我这里 ...

  2. 「Luogu P5603」小O与桌游

    题目链接 戳我 \(Solution\) 我们来分析题目. 实际上就是求一个拓扑序满足拓扑序的前缀最大值最多/最少 对于第一种情况,很明显一直选当前能选的最小的是最优的对吧.因为你需要大的尽可能多.用 ...

  3. C++ 学习时的错误记录

    1. 关于C++相关的文件扩展名 c++程序中的头文件扩展名包括: .h .hpp .hxx C++程序中源文件的扩展名包括: .cc .cpp .cxx 2.C++程序编译过程 3. 处理错误 4. ...

  4. BitmapRegionDecoder

    Android加载大图——BitmapRegionDecoder(转)   BitmapRegionDecoder,从API10就引入了.如下图:   NPONRY0T35GE$13{254X8Z1. ...

  5. 微信小程序之数据缓存和数据获取

    在一个微信小程序中 避免不了的就是在多个页面获取数据,而且还会在不同的页面获取相同的数据,写起来就是一大坨,看着就不愉快 那么今天说一下 这个方法 wx.setStorage(OBJECT) 这个方法 ...

  6. n个骰子可能的点数和

    让后面的点数比前面的大 package touzi; public class Touzi { public static void main(String[] args) { // TODO Aut ...

  7. Struts数据回显和模型驱动

    prams拦截器,可以把请求数据自动填充的action的属性中 举例1: JSP <input type=text name=userName /> <input type=text ...

  8. Android studio 自动导入(全部)包 import (转)

    原文: https://blog.csdn.net/buaaroid/article/details/44979629 1.      Android studio 只有import单个包的快捷键:A ...

  9. 浅谈JS中 reduce() 的用法

    过去有很长一段时间,我一直很难理解 reduce() 这个方法的具体用法,平时也很少用到它.事实上,如果你能真正了解它的话,其实在很多地方我们都可以用得上,那么今天我们就来简单聊聊JS中 reduce ...

  10. 八十三:redis之redis的使用场景和安装

    使用场景1.登录会话存储,存储在redis中,与mamcached相比,数据不会丢失2.排行榜.计数器:比如一些秀场类的项目,经常会有一些前多少名的主播排行榜,还有一些文章阅读量.或者点赞数等3.作为 ...