一道线程同步面试题

一道线程同步面试题

package com.wsy.model;

/**
 * 完毕主线程循环10次,接着子线程循环5次,如此往复10次
 * 
 * @author wsy
 * 
 */
public class TraditionalThreadCommunication {
	static Bussiness bussiness = new Bussiness();

	public static void main(String[] args) {
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= 10; i++) {
					bussiness.sub(i);
				}
			}
		}).start();
		
		for (int i = 1; i <= 10; i++) {
			bussiness.main(i);
		}
		
	}
}

class Bussiness {
	static boolean isThreadSub = true;

	public synchronized void sub(int i) {
		
		while (!isThreadSub) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		for (int j = 1; j <= 10; j++) {
			System.out.println("sub: " + j + " of loop " + i);
		}

		isThreadSub = false;
		this.notify();
		
	}

	public synchronized void main(int i) {
		
		while (isThreadSub) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		for (int j = 1; j <= 5; j++) {
			System.out.println("main: " + j + " of loop " + i);
		}

		isThreadSub = true;
		this.notify();
		
	}
}

输出结果:

sub: 1 of loop 1
sub: 2 of loop 1
sub: 3 of loop 1
sub: 4 of loop 1
sub: 5 of loop 1
sub: 6 of loop 1
sub: 7 of loop 1
sub: 8 of loop 1
sub: 9 of loop 1
sub: 10 of loop 1
main: 1 of loop 1
main: 2 of loop 1
main: 3 of loop 1
main: 4 of loop 1
main: 5 of loop 1
sub: 1 of loop 2
sub: 2 of loop 2
sub: 3 of loop 2
sub: 4 of loop 2
sub: 5 of loop 2
sub: 6 of loop 2
sub: 7 of loop 2
sub: 8 of loop 2
sub: 9 of loop 2
sub: 10 of loop 2
main: 1 of loop 2
main: 2 of loop 2
main: 3 of loop 2
main: 4 of loop 2
main: 5 of loop 2
sub: 1 of loop 3
sub: 2 of loop 3
sub: 3 of loop 3
sub: 4 of loop 3
sub: 5 of loop 3
sub: 6 of loop 3
sub: 7 of loop 3
sub: 8 of loop 3
sub: 9 of loop 3
sub: 10 of loop 3
main: 1 of loop 3
main: 2 of loop 3
main: 3 of loop 3
main: 4 of loop 3
main: 5 of loop 3
sub: 1 of loop 4
sub: 2 of loop 4
sub: 3 of loop 4
sub: 4 of loop 4
sub: 5 of loop 4
sub: 6 of loop 4
sub: 7 of loop 4
sub: 8 of loop 4
sub: 9 of loop 4
sub: 10 of loop 4
main: 1 of loop 4
main: 2 of loop 4
main: 3 of loop 4
main: 4 of loop 4
main: 5 of loop 4
sub: 1 of loop 5
sub: 2 of loop 5
sub: 3 of loop 5
sub: 4 of loop 5
sub: 5 of loop 5
sub: 6 of loop 5
sub: 7 of loop 5
sub: 8 of loop 5
sub: 9 of loop 5
sub: 10 of loop 5
main: 1 of loop 5
main: 2 of loop 5
main: 3 of loop 5
main: 4 of loop 5
main: 5 of loop 5
sub: 1 of loop 6
sub: 2 of loop 6
sub: 3 of loop 6
sub: 4 of loop 6
sub: 5 of loop 6
sub: 6 of loop 6
sub: 7 of loop 6
sub: 8 of loop 6
sub: 9 of loop 6
sub: 10 of loop 6
main: 1 of loop 6
main: 2 of loop 6
main: 3 of loop 6
main: 4 of loop 6
main: 5 of loop 6
sub: 1 of loop 7
sub: 2 of loop 7
sub: 3 of loop 7
sub: 4 of loop 7
sub: 5 of loop 7
sub: 6 of loop 7
sub: 7 of loop 7
sub: 8 of loop 7
sub: 9 of loop 7
sub: 10 of loop 7
main: 1 of loop 7
main: 2 of loop 7
main: 3 of loop 7
main: 4 of loop 7
main: 5 of loop 7
sub: 1 of loop 8
sub: 2 of loop 8
sub: 3 of loop 8
sub: 4 of loop 8
sub: 5 of loop 8
sub: 6 of loop 8
sub: 7 of loop 8
sub: 8 of loop 8
sub: 9 of loop 8
sub: 10 of loop 8
main: 1 of loop 8
main: 2 of loop 8
main: 3 of loop 8
main: 4 of loop 8
main: 5 of loop 8
sub: 1 of loop 9
sub: 2 of loop 9
sub: 3 of loop 9
sub: 4 of loop 9
sub: 5 of loop 9
sub: 6 of loop 9
sub: 7 of loop 9
sub: 8 of loop 9
sub: 9 of loop 9
sub: 10 of loop 9
main: 1 of loop 9
main: 2 of loop 9
main: 3 of loop 9
main: 4 of loop 9
main: 5 of loop 9
sub: 1 of loop 10
sub: 2 of loop 10
sub: 3 of loop 10
sub: 4 of loop 10
sub: 5 of loop 10
sub: 6 of loop 10
sub: 7 of loop 10
sub: 8 of loop 10
sub: 9 of loop 10
sub: 10 of loop 10
main: 1 of loop 10
main: 2 of loop 10
main: 3 of loop 10
main: 4 of loop 10
main: 5 of loop 10

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119273.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 一步使你的asp.net网站在手机浏览器上全屏显示

    一步使你的asp.net网站在手机浏览器上全屏显示

  • C++虚函数详解

    C++虚函数详解C++虚函数详解前言C++的特性使得我们可以使用函数继承的方法快速实现开发,而为了满足多态与泛型编程这一性质,C++允许用户使用虚函数**(virtualfunction)来完成运行时决议这一操作,这与一般的编译时决定**有着本质的区别。虚函数表实现原理虚函数的实现是由两个部分组成的,虚函数指针与虚函数表。虚函数指针虚函数指针**(virtualfunctionpointer)*…

  • navicat15 密钥自动激活吗_在线激活

    (navicat15 密钥自动激活吗)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

  • original_keras_version = f.attrs[‘keras_version‘].decode(‘utf8‘)

    original_keras_version = f.attrs[‘keras_version‘].decode(‘utf8‘)File”/home/liqiang/anaconda3/envs/tensorflow1.8/lib/python3.6/site-packages/keras/engine/saving.py”,line1004,inload_weights_from_hdf5_grouporiginal_keras_version=f.attrs[‘keras_version’].decode(‘utf8’)AttributeError:’str’objecthasnoatt…

    2022年10月18日
  • (ExcelVBA编程入门范例)

    (ExcelVBA编程入门范例)很喜爱VBA,喜欢使用她对Excel操作实现所需的功能,更喜欢使用VBA控制Excel以及实现结果后的那种感觉。一直都想对ExcelVBA进行系统的整理和归纳,但由于对Excel及VBA了解得不够深入,总觉得无从下手。再加上又是利用少得可怜的业余时间进行学习,时断时续,学习的主线和思路也经常因为工作或其它的事情而打断。但喜欢学习的人总会挤得出时间来的,要想掌握或者是精通一门知识和技术不能有任何借口

  • sqlite如何安装_安装路由器的详细步骤

    sqlite如何安装_安装路由器的详细步骤1.安装sudoapt-getinstallsqlite32.可执行程序sqlite3在/bin目录下,使用sqlite3+空格+数据库文件,可打开数据库3.使用.database查看已连接的数据库会话4.使用.tables查看数据库中的表5.执行SQL进行增删查改Linux下的SQLite数据库查看工具:SQLiteBrowser安装:sudoaptinstallsqlitebrowser-ywindows下的SQLite数据库查看工具:SQLi…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号