写2个线程,一个打印1-52,一个打印A-Z,打印顺序是12A34B。。。(采用同步代码块和同步方法两种同步方法)
1.同步方法
package Synchronized;
/************************************同步方法****************************************/
public class PrintTest {
public static void main(String[] args)
{
Print p = new Print();
Thread t1 = new PrintNumber(p);
Thread t2 = new PrintWord(p);
t1.start();
t2.start();
}
} class PrintNumber extends Thread {//打印数字线程
private Print p; public PrintNumber(Print p) {
this.p = p;
} public void run() {
for (int i = 0; i < 26; i++) {
p.printNumber();
}
}
} class PrintWord extends Thread {//打印字母线程
private Print p; public PrintWord(Print p) {
this.p = p;
} public void run() {
for (int i = 0; i < 26; i++) {
p.printWord();
}
}
} class Print { //同步监视器是Print类
private int i = 1;
private char j = 'A'; public Print() {
} public synchronized void printNumber() {//同步方法
System.out.print(String.valueOf(i) + String.valueOf(i + 1));
i += 2;
notifyAll(); //先唤醒其他进程,再阻塞本进程,如果顺序颠倒了,进程阻塞后不能再唤醒其他进程
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} public synchronized void printWord() {
System.out.print(j);
j++;
notifyAll();
try
{
if (j <= 'Z')//输出Z之后就不用再等待了。
{ wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
} }
}
2.同步代码块package threaddemo;
/**
* <写两个线程,一个线程打印1-52,另一个线程打印字母A-Z。打印 顺序为12A34B56C……5152Z>
*
*/
/*****************************************同步代码块*********************************************/
public class ThreadDemo
{
// 测试
public static void main(String[] args) throws Exception
{
Object obj = new Object();
// 启动两个线程
Thread1 t1 = new Thread1(obj); Thread2 t2 = new Thread2(obj); t1.start();
t2.start();
} } // 一个线程打印1-52
class Thread1 extends Thread
{
private Object obj; public Thread1(Object obj)
{
this.obj = obj;
} public void run()
{
synchronized (obj)
{
// 打印1-52
for (int i = 1; i < 53; i++)
{
System.out.print(i + " ");
if (i % 2 == 0)
{
// 不能忘了 唤醒其它线程
obj.notifyAll();
try
{
obj.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
} } } // 另一个线程打印字母A-Z
class Thread2 extends Thread
{
private Object obj; public Thread2(Object obj)
{
this.obj = obj;
} public void run()
{
synchronized (obj) //同步监视器是obj类,同步代码块是写在run方法里面的。
{
// 打印A-Z
for (int i = 0; i < 26; i++)
{
System.out.print((char)('A' + i) + " ");
// 不能忘了 唤醒其它线程
obj.notifyAll();
try
{
// 最后一个就不要等了
if (i != 25)
{
obj.wait();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
} } }
} }
下面是我后来写的。自习区分一下,因为主程序只有两个线程相互交替,所以是没有必要设置flag的。只有很多进程交互的时候,才有必要设置flag,并且我是通过flag来判断切换进程的,所以循环次数是52次,而不是26次。
public class test1
{
public static void main(String[] args) {
Print p = new Print();
new PrintNumber(p).start();
new PrintWord(p).start();
}
}
class Print
{
private boolean flag = false;
public int num = 1;
public char chr = 'A';
public synchronized void printNumber()
{
try
{
if(flag)
{
if(num <= 52)
{
wait();
}
}
else
{
System.out.print(num);
System.out.print(num + 1);
num += 2;
flag = true;
notify();
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
} public synchronized void printWord()
{
try
{
if(!flag)
{
if(chr <= 'Z')
{
wait();
}
}
else
{
System.out.print(chr);
chr += 1;
flag = false;
notify();
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class PrintNumber extends Thread
{
Print p;
PrintNumber(Print p)
{
this.p = p;
}
public void run()
{
for(int i = 0; i < 52; i ++)
{
p.printNumber();
}
}
}
class PrintWord extends Thread
{
Print p;
PrintWord(Print p)
{
this.p = p;
}
public void run()
{
for(int i = 0; i < 52; i ++)
{
p.printWord();
}
}
}
实现Runnable接口
public class test2 {
public static void main(String[] args) {
Print p = new Print();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 26; i ++)
{
p.printNum();
}
}
}).start();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 26; i ++)
{
p.printWord();
}
}
}).start();
}
}
class Print
{
char chr = 'A';
int num = 1;
public synchronized void printNum()
{
System.out.print(num);
System.out.print(num + 1);
num += 2;
notify();
try{
wait();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
} }
public synchronized void printWord()
{
System.out.print(chr);
chr += 1;
notify();
try{
if(chr <= 'Z')
wait();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
写2个线程,一个打印1-52,一个打印A-Z,打印顺序是12A34B。。。(采用同步代码块和同步方法两种同步方法)的更多相关文章
- java线程基础巩固---同步代码块以及同步方法之间的区别和关系
在上一次中[http://www.cnblogs.com/webor2006/p/8040369.html]采用同步代码块的方式来实现对线程的同步,如下: 对于同步方法我想都知道,就是将同步关键字声明 ...
- 对象及变量的并发访问(同步方法、同步代码块、对class进行加锁、线程死锁)&内部类的基本用法
主要学习多线程的并发访问,也就是使得线程安全. 同步的单词为synchronized,异步的单词为asynchronized 同步主要就是通过锁的方式实现,一种就是隐式锁,另一种是显示锁Lock,本节 ...
- JAVA之旅(十三)——线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this
JAVA之旅(十三)--线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this 我们继续上个篇幅接着讲线程的知识点 一.线程的安全性 当我们开启四个窗口(线程 ...
- Java基础8-多线程;同步代码块
作业解析 利用白富美接口案例,土豪征婚使用匿名内部类对象实现. interface White{ public void white(); } interface Rich{ public void ...
- 线程执行synchronized同步代码块时再次重入该锁过程中抛异常,是否会释放锁
一个线程执行synchronized同步代码时,再次重入该锁过程中,如果抛出异常,会释放锁吗? 如果锁的计数器为1,抛出异常,会直接释放锁: 那如果锁的计数器为2,抛出异常,会直接释放锁吗? 来简单测 ...
- Java 基础 线程的Runnable接口 /线程的同步方法 /同步代码块
笔记: /**通过 Runnable接口来实现多线程 * 1. 创建一个实现runnable 接口的类 * 2. 在类中实现接口的run() 抽象方法 * 3. 创建一个runnable 接口实现类的 ...
- 廖雪峰Java11多线程编程-2线程同步-1同步代码块
1.线程安全问题 多个线程同时运行,线程调度由操作系统决定,程序本身无法决定 如果多个线程同时读写共享变量,就可能出现问题 class AddThread extends Thread{ public ...
- 线程的同步机制:同步代码块&同步方法
解决存在的线程安全问题:打印车票时出现重票,错票 使用同步代码块的解决方案 TestWindow2 package com.aff.thread; /* 使用实现Runnable接口的方式,售票 存在 ...
- 线程同步 synchronized 同步代码块 同步方法 同步锁
一 同步代码块 1.为了解决并发操作可能造成的异常,java的多线程支持引入了同步监视器来解决这个问题,使用同步监视器的通用方法就是同步代码块.其语法如下: synchronized(obj){ // ...
随机推荐
- 前端Js传递数组至服务器端
$("#delete").click(function () { var ID = ""; var ary = []; var bb = document.ge ...
- EasyNVR互联网监控直播分发出RTMP、HLS、HTTP-FLV视频流说明介绍
背景需求 需求比视频流协议更重要,你想要什么,什么可以满足你的需求,这个很大程度上是需求在前,选择使用什么视频流是比较靠后的. 目前Easy系列互联网直播服务将全线支持HLS.RTMP.HTP-FLV ...
- 创建String字符串的方式与区别
Java中创建一个字符串的方式有很多种,常见如: String s = new String("riqi"); String s = "riqi"; 但两者有什 ...
- Truthy Falsy
https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy falsy(虚值)是在 Boolean 上下文中已认定可转换为‘假‘的值. JavaS ...
- 前端基础 & Bootstrap框架
Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局,并且 ...
- 常用算法及其Python实现
冒泡排序 def bubble_sort(li): for i in range(len(li)-1): # i表示第几趟 for j in range(len(li)-i-1 ...
- Python 获取文件路径及文件目录
import os print (os.path.dirname(__file__)) print (os.path.abspath(__file__)) print (os.path.abspath ...
- docker helloworld
阿里云镜像: docker官网 docker官方安装文档 配置阿里云镜像后,重启服务,以及检测服务是否正确启动 docker version.info.--help linux帮助命令man ls 镜 ...
- iOS 系统认知 debug distribution release 和 #ifdef DEBUG
debug:调试模式 有调试信息 线下 release: 无调试信息 经过了编译优化 发布 给用户使用的 线上模式 一般 工程项目 都是自带 上述两种配置结构 还有出现 distribution: ...
- 导入Frameworks 死活引用不Liao头文件
向工程中拖入或add file时可能会出现Frameworks导入,但是在添加该Frameworks后却引用不到相应的头文件 打开工程文件目录发现frameworks所在的路径并不存在,而是直接在工程 ...