SlideShare a Scribd company logo
Лекция 3. «Message System»
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
Java осень 2012 лекция 3
public class Address {
static private AtomicInteger abonentIdCreator = new AtomicInteger();
final private int abonentId;
public Address(){
this.abonentId = abonentIdCreator.incrementAndGet();
}
public int getAbonentId() {
return abonentId;
}
}
public interface Abonent {
Address getAddress();
}
public abstract class Msg {
final private Address from;
final private Address to;
public Msg(Address from, Address to){
this.from = from;
this.to = to;
}
protected Address getFrom(){
return from;
}
protected Address getTo(){
return to;
}
public abstract void exec(Abonent abonent);
}
public abstract class MsgToFrontend extends Msg {
public MsgToFrontend(Address from, Address to) {
super(from, to);
}
public void exec(Abonent abonent) {
if(abonent instanceof Frontend){
exec((Frontend)abonent);
}
}
public abstract void exec(Frontend frontend);
}
public class MsgUpdateUserId extends MsgToFrontend {
final private int sessionId;
final private int userId;
public GameInfoMsg(Address from, Address to, int sessionId, int userId) {
super(from, to);
this. sessionId = sessionId;
this. userId = userId;
}
public void exec(Frontend frontend) {
frontend.updateUserId(sessionId, userId);
}
}
private Map<Address, ArrayBlockingQueue<Msg>> messages
= new HashMap<Address, ArrayBlockingQueue<Msg>>();
public void sendMessage(Msg message){
Queue<Msg> messageQueue = messages.get(message.getTo());
messageQueue.add(message);
}
public void execForAbonent(Abonent abonent) {
Queue<Msg> messageQueue = messages.get(abonent.getAddress());
while(!messageQueue.isEmpty()){
Msg message = messageQueue.poll();
message.exec(abonent);
}
}
Java осень 2012 лекция 3
Java осень 2012 лекция 3
public void run() {
while (true) {
messageSystem.execForAbonent(this);
Thread.sleep(TICK_TIME);
}
}
Java осень 2012 лекция 3
Java осень 2012 лекция 3

More Related Content

DOCX
การวาง Link แบบ Transpose
PPTX
Java весна 2013 лекция 6
PDF
Бизнес и системный анализ весна 2013 лекция 1
PDF
C++ осень 2012 лекция 8
PPTX
Java весна 2013 лекция 8
PPTX
Java осень 2012 лекция 6
PPTX
Java осень 2012 лекция 4
PPTX
Java весна 2013 лекция 7
การวาง Link แบบ Transpose
Java весна 2013 лекция 6
Бизнес и системный анализ весна 2013 лекция 1
C++ осень 2012 лекция 8
Java весна 2013 лекция 8
Java осень 2012 лекция 6
Java осень 2012 лекция 4
Java весна 2013 лекция 7

Similar to Java осень 2012 лекция 3 (14)

PPTX
Java весна 2013 лекция 3
PDF
Creating a Facebook Clone - Part XX.pdf
PPTX
Java осень 2012 лекция 2
PPTX
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
PPTX
#5 (Remote Method Invocation)
PPTX
Refactoring - Mejorando el diseño del código existente
KEY
How to Start Test-Driven Development in Legacy Code
PDF
Creating a Facebook Clone - Part XX - Transcript.pdf
PDF
Lombokの紹介
PDF
ikh331-06-distributed-programming
PDF
3. Объекты, классы и пакеты в Java
PDF
Speed up your Web applications with HTML5 WebSockets
PPTX
Java весна 2013 лекция 2
DOCX
Week 12 code
Java весна 2013 лекция 3
Creating a Facebook Clone - Part XX.pdf
Java осень 2012 лекция 2
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
#5 (Remote Method Invocation)
Refactoring - Mejorando el diseño del código existente
How to Start Test-Driven Development in Legacy Code
Creating a Facebook Clone - Part XX - Transcript.pdf
Lombokの紹介
ikh331-06-distributed-programming
3. Объекты, классы и пакеты в Java
Speed up your Web applications with HTML5 WebSockets
Java весна 2013 лекция 2
Week 12 code
Ad

More from Technopark (20)

PDF
Лекция 11. Вычислительная модель Pregel
PDF
Лекция 14. Hadoop в Поиске Mail.Ru
PDF
Лекция 13. YARN
PDF
Лекция 12. Spark
PDF
Лекция 10. Apache Mahout
PDF
Лекция 9. ZooKeeper
PDF
Лекция 7. Введение в Pig и Hive
PDF
Лекция 6. MapReduce в Hadoop (графы)
PDF
Лекция 5. MapReduce в Hadoop (алгоритмы)
PDF
Лекция 4. MapReduce в Hadoop (введение)
PDF
Лекция 3. Распределённая файловая система HDFS
PDF
Лекция 2. Основы Hadoop
PDF
Лекция 1. Введение в Big Data и MapReduce
PPTX
СУБД 2013 Лекция №10 "Нереляционное решение в области баз данных — NoSQL"
PPT
СУБД 2013 Лекция №10 "Нереляционное решение в области баз данных — NoSQL" Час...
PPTX
СУБД 2013 Лекция №9 "Безопасность баз данных"
PPTX
СУБД 2013 Лекция №8 "Конфигурирование базы данных"
PPTX
СУБД 2013 Лекция №7 "Оптимизация запросов и индексирование"
PPTX
СУБД 2013 Лекция №5 "Определение узких мест"
PPTX
СУБД 2013 Лекция №6 "Профилирование запросов. Сложноструктурированные SQL-зап...
Лекция 11. Вычислительная модель Pregel
Лекция 14. Hadoop в Поиске Mail.Ru
Лекция 13. YARN
Лекция 12. Spark
Лекция 10. Apache Mahout
Лекция 9. ZooKeeper
Лекция 7. Введение в Pig и Hive
Лекция 6. MapReduce в Hadoop (графы)
Лекция 5. MapReduce в Hadoop (алгоритмы)
Лекция 4. MapReduce в Hadoop (введение)
Лекция 3. Распределённая файловая система HDFS
Лекция 2. Основы Hadoop
Лекция 1. Введение в Big Data и MapReduce
СУБД 2013 Лекция №10 "Нереляционное решение в области баз данных — NoSQL"
СУБД 2013 Лекция №10 "Нереляционное решение в области баз данных — NoSQL" Час...
СУБД 2013 Лекция №9 "Безопасность баз данных"
СУБД 2013 Лекция №8 "Конфигурирование базы данных"
СУБД 2013 Лекция №7 "Оптимизация запросов и индексирование"
СУБД 2013 Лекция №5 "Определение узких мест"
СУБД 2013 Лекция №6 "Профилирование запросов. Сложноструктурированные SQL-зап...
Ad

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Approach and Philosophy of On baking technology
PPTX
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
Approach and Philosophy of On baking technology
sap open course for s4hana steps from ECC to s4

Java осень 2012 лекция 3

  • 20. public class Address { static private AtomicInteger abonentIdCreator = new AtomicInteger(); final private int abonentId; public Address(){ this.abonentId = abonentIdCreator.incrementAndGet(); } public int getAbonentId() { return abonentId; } } public interface Abonent { Address getAddress(); }
  • 21. public abstract class Msg { final private Address from; final private Address to; public Msg(Address from, Address to){ this.from = from; this.to = to; } protected Address getFrom(){ return from; } protected Address getTo(){ return to; } public abstract void exec(Abonent abonent); }
  • 22. public abstract class MsgToFrontend extends Msg { public MsgToFrontend(Address from, Address to) { super(from, to); } public void exec(Abonent abonent) { if(abonent instanceof Frontend){ exec((Frontend)abonent); } } public abstract void exec(Frontend frontend); }
  • 23. public class MsgUpdateUserId extends MsgToFrontend { final private int sessionId; final private int userId; public GameInfoMsg(Address from, Address to, int sessionId, int userId) { super(from, to); this. sessionId = sessionId; this. userId = userId; } public void exec(Frontend frontend) { frontend.updateUserId(sessionId, userId); } }
  • 24. private Map<Address, ArrayBlockingQueue<Msg>> messages = new HashMap<Address, ArrayBlockingQueue<Msg>>(); public void sendMessage(Msg message){ Queue<Msg> messageQueue = messages.get(message.getTo()); messageQueue.add(message); } public void execForAbonent(Abonent abonent) { Queue<Msg> messageQueue = messages.get(abonent.getAddress()); while(!messageQueue.isEmpty()){ Msg message = messageQueue.poll(); message.exec(abonent); } }
  • 27. public void run() { while (true) { messageSystem.execForAbonent(this); Thread.sleep(TICK_TIME); } }