Java весна 2013 лекция 3

302 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
302
On SlideShare
0
From Embeds
0
Number of Embeds
70
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java весна 2013 лекция 3

  1. 1. public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } public final int incrementAndGet() { while (true) { int current = get(); //get() возвращает текущее значение (volatile) int next = current + 1; if (compareAndSet(current, next)) return next; } }
  2. 2. ― ― ― ― ―
  3. 3. ― ― ― ― ―
  4. 4.
  5. 5. public class Address { static private AtomicInteger abonentIdCreator = new AtomicInteger(); final private int abonentId; public Address(){ this.abonentId = abonentIdCreator.incrementAndGet(); } public int hashCode() { return abonentId; } } public interface Abonent { Address getAddress(); }
  6. 6. 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); }
  7. 7. 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); }
  8. 8. 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); } }
  9. 9. Msg MsgToAS MsgToFrontend MsgUpdateUserIdMsgGetUserId - Address from - Address to - String name - Integer sessionId - Integer sessionId - Integer userId
  10. 10. private Map<Address, ConcurrentLinkedQueue<Msg>> messages = new HashMap<Address, ConcurrentLinkedQueue<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); } }
  11. 11. public class AddressService { private Map<Class<?>, Address> addresses = new HashMap<Class<?>, Address>(); public Address getAddress(Class<?> abonentClass) { return addresses.get(abonentClass); } public void setAddress(Abonent abonent) { addresses.put(abonent.getClass(), abonent.getAddress()); } }
  12. 12. public void run() { while (true) { messageSystem.execForAbonent(this); Thread.sleep(TICK_TIME); } }
  13. 13. 14. Плюсы и минусы многопоточных приложений 15. Способы взаимодействия потоков 16. java.util.concurrent 17. MessageSystem. Address и Abonent

×