java 如何编写多线程的代码
线程是干活的
所以线程一定是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 如何编写多线程的代码的更多相关文章
- 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁
什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...
- java 22 - 9 多线程之 代码实现的方式2
多线程的代码实现: 方式2:实现Runnable接口 步骤: A:自定义类MyRunnable实现Runnable接口 B:重写run()方法 C:创建MyRunnable类的对象 D:创建Threa ...
- java 22 - 4 多线程的代码实现的方式1
需求:我们要实现多线程的程序. 如何实现呢? 由于线程是依赖进程而存在的,所以我们应该先创建一个进程出来. 而进程是由系统创建的,所以我们应该去调用系统功能创建一个进程. Java是不能直接调用系统功 ...
- Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)
Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...
- java如何编写多线程
1.如何实现多线程 1.1实现Runnable接口,实现run()方法. public class Main4 implements Runnable { public static void mai ...
- 使用java语言编写窗口按钮
使用java语言编写窗口按钮 代码如下: package Day08; import java.awt.FlowLayout; import javax.swing.JButton;import ja ...
- 编写高质量代码改善java程序的151个建议——导航开篇
2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...
- Java编写高质量代码改善程序的151个建议
第一章 Java开发中通用的方法和准则 建议1:不要在常量和变量中出现易混淆的字母: (i.l.1:o.0等). 建议2:莫让常量蜕变成变量: (代码运行工程中不要改变常量值). 建议3:三元操作符 ...
- 编写高质量代码:改善Java程序的151个建议 --[117~128]
编写高质量代码:改善Java程序的151个建议 --[117~128] Thread 不推荐覆写start方法 先看下Thread源码: public synchronized void start( ...
随机推荐
- HDU 5795 A Simple Nim ——(Nim博弈 + 打表)
题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作. 分析:打表找sg值的规律即可. 感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~ 打 ...
- arxiv-sanity使用指南
使用介绍 https://bookdown.org/wshuyi/intro-to-scientific-writings4/reading.html#find-article-with-ai
- shell编程-定时任务(备份数据库)
计划任务定时备份,删除等操作: #crontab -e #注意 会区分用户 默认在root用户登录用的是root权限用户的计划任务, 如果想在postgres备份 应使用postgres用户权限, 设 ...
- logback条件日志配置
logback支持条件日志配置,支持在测试环境和正式环境使用不同的参数启用不同的日志配置,从而避免手动修改日志配置文件.项目除了引入logback的包以外,还需要引入构件org.codehaus.ja ...
- 【好书推荐】9、安卓Andorid编程吐血整理100+本
点开即可
- HearthstoneBot
https://github.com/ChuckFork/HearthstoneBot Sigmund Card game automation framework Hooks game and lo ...
- LC 660. Remove 9 【lock, hard】
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- url protocol
首先注册服务 方法1,保存为reg文件直接执行,需要按需修改路径 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\EasyPrint] ...
- 本地安装完oracle,plsql 连接不上
原因是本地装的oracle版本是12c,oracle客户端装的是11,所以连接不上,没有匹配的验证协议 客户端换成12,成功连接.
- np.where()
numpy.where (condition[, x, y]) numpy.where()两种用法 1. np.where(condition, x, y) 满足条件(condition),输出x,不 ...