Code Monkey home page Code Monkey logo

talk's Introduction

talk

用java实现socket聊天室

遇到多线程变量共享问题

参考如下连接解决的

代码在Room.java 这个属性的定义

private volatile Queue<String> transpondMessage = new LinkedList<String>();

以及这里的应用

	public void notifyMessage() {
		while (true) {
			
			String message = this.transpondMessage.poll();
//			System.out.println("11111111");
			if (message == null) {
				continue;
			}
//			System.out.println("22222222");
//			System.out.println(message);
//			System.out.println(this.userList.size());
			this.userList.forEach((uid, user) -> {
				user.receiveMessageFromRoom(message);
			});
		}
	}

总结一下 : volatile

volatile修饰的变量,可以再多线程之间,立刻更新,但是不能保证原子性。

如果volatile修饰的变量,再加上get方法和set锁的应用,可以保证原子性,并且多个线程可以立马看到,但是这样做的性能那就更更更低了 还不如直接用线程安全的库。

那为什么我这次编写还volatile和锁都用上呢? 因为我编写的这个聊天室模型有点特殊, set方法保证多线程原子性操作就行, 而对于get方法,只有一个线程读取这个队列,没有出现资源竞争, 所以再get方法不需要加上锁,也能保证原子性(头部弹出),只需要保证set再多线程添加的数据能立马更新就行, 所以get方法不需要加锁,但是要有volatile修饰

talk's People

Contributors

flashfeifei avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.