Java Concurrency


- synchronized

- lock, condition
- synchronizedList/Set/Map
- Optimistic Lock: AtomicXXX

- Threadpool
Wait, Notify(All)
- check condition again after wake up and in a loop
- while(condition is not true) obj.wait();
- missed signals
- spurious wakeups


Lock
- replaces synchronized
Condition
- suspend thread's execution, until the condition is true.
- a lock can have multiple (conditions) wait-sets per object
- replaces monitor methods
ReentrantLock

- interruptible, timeout(tryAcquire)


Semaphore

- n permits, acquire, release
- Limiting concurrent access

CountDownLatch
- countDown, await
- can't reuse
- work even if countDown is called before await (unlike wait, notify - missing notification)

CyclicBarrier
- synchronize some tasks at a common point
- reusable

Double-checked locking

- check null again inside the critical block

Volatile

- modification to the field is immediately seen by other threads
- not cached in registers, force main memory access
- no reorder
- happens-before
any memory writes by one statement are visible to another statement.
Use Volatile
- When Writes Do Not Depend on Its Current Value
- Use Volatile Fields for Reading and Locks for Writing
Implementing CopyOnWriteArrayList
- private transient volatile Object[] array;

- Use lock in set(index, element)

AtomicXXX
- CAS
- One instruction, retry infinitely until it succeeds


ConcurrentHashMap
ConcurrentLinkedQueue
- CAS, headNode, taiNode

How to Prevent Deadlock

- same lock ordering
- lock timeout, tryAcquire

Implementing
- Threads that print odd, even numbers
BoundedBuffer
BlockingQueue

- drainTo

- Condition: notFull, notEmpty
Implement Lock using synchronized
Implement ReadWriteLock
Implementing (Thread Safe) Stack
- AtomicReference, CAS
Develop a software to count number of events in last 5 mins
- AtomicLongArray, PositionUpdater
counter.set((pos + 1)%GRANULARITY, 0);
pos = (pos + 1)%GRANULARITY;

Write a program which keeps min and max value in threadsafe way

- AtomicInteger
H2O
Blocking Queue
LinkedBlockingQueue
ArrayBlockingQueue


PriorityBlockingQueue
DelayQueue


ThreadPool

newCachedThreadPool
- Use SynchronousQueue, may use too many threads
newFixedThreadPool, newSingleThreadExecutor
- Use LinkedBlockingQueue, may use too much memory
- Be cautious about unbounded resource - thread, memory

How to Ace System Design


Always think about different approaches, think better approaches and say it.

Step 1:
Ask questions, don't assume (at lease check your assumption)
- whether this is algorithm or design problem?
Step 2: System interface definition
- interface, main functions
Step 3: High level design with some diagram
Back-of-the-envelope capacity estimation
Defining the data model
Identifying and resolving bottlenecks

Make a List:
- main functions of the system
- locate bottleneck + how to scale

Unique challenges of these features/system
Non-function features
Unique features
Rate limiter - scalable, availability,  DDos
Priority - Critical vs Non-Critical
Load smoothy or spiky(predictable or not)?

Use cases analysis

- How client is going to use it

Locate the problem of the current design 
- show you are aware of them
How to optimize it if possible
How to scale

Separate of concerns
Separate or not
Rate limiter - embedded in application or seperated
Tiny url - separate write, read api

Follow the user case, end to end What's the bottleneck, the challenge
How to scale
How to handle change - node added/removed/crashed



The features of these functions
- read/write ratio, read heavy or write heavy or both

How to use it
Senario
Better user experience
User perceived experience/speed
- cheat, approximate
Thinking from client/user perspective
How they use it, what they would like to know

Extra functions that may be related or needed

API Design
Idempotency
Idempotency key
Back pressure exponential backoff
Randomness, Jitter

Fault-tolerant
Retry
Timeout
Rate limit

Degrade
Separation of services
Priority - Critical vs Non-Critical

Offline tasks
Use data from offline tasks

- topn followers, interest getCore

限流
削峰
异步处理
用户体验

用户交互

及时拒绝,防止雪崩


How to Conduct System Design Interview
In memory -> persistence

Lesson Learned from Investing


Stock

Discipline

Be patient

  • Don’t buy stocks if you need the money in a limited time(within 3 months)

Know your stocks

  • history
  • how volatile it is, whether it goes up/down a lot suddenly in the past
  • news

Know the trending

  • whether shorting or buying
  • market is volatile or going up/down
  • political

Diversity

  • Don’t put all eggs in one basket
  • Don’t put more than 1/4 or more than X into any stock
  • It’s not diversity if you buy A with 50% of your money, and some other stocks. You put too much bet on one stock.

Don’t invest all your money

  • always leave plenty of buffer
  • if some stock drop more than 10 percentage, you still have money left to maybe buy more

Be very careful when use margin

  • It’s very addictive, you intend to use it (even you know its not the best opportunity, because if you don’t use it, it may be gone)
  • Dangerous
  • More money, more risk, harder to manage
  • Not predictable
  • Do’t use it unless its really a good opportunity

How to choose a stock

  • Know who are buying/selling the stock
  • don’t be the only few who is buying the stock, or someone are selling the stock (a lot), you are the one of the few who is buying it
  • the volume, don’t buy stock that’s hard to sell it
  • only because the price drops a lot (know why)

Don’t buy the following stocks

  • not familiar
  • only because others recommend it (do your own research)
  • trash company/stock
  • the volume is very low
    • hard to sell, and it may drop a lot very quickly (manipulate by someone, as only few people are buying it)
  • you don’t know why it goes down or up, when it goes up or down, you don’t know how long it will continue
  • you will lose the leverage

When to buy

  • If a stock goes down, don’t just keep buying, buy it only when it drops down 5 percentage etc
  • When you already buy a lot, don’t buy it again when it just drops a little, buy it only when it drops a lot
  • Don’t think buy more at a slight lower price to even the cost, and then sell it quickly

When to sell

Case: Sell it with zero gain or small percentage loss

  • Don’t hope it will (continue to) go up, then you will sell it when you gain (some or a little) profit, it may not in near term
  • Case: When you need sell some stocks (need the money, or you need buy more another stock to cover the loss etc)

How to sell

  • Need a plan
  • Based on how likely it will continue to go up, and how much it may go up
  • Based on how much money in this stock
  • Sell by percentage

Critical moment

For some stocks, it only pops up in very limited time, grasp this opportunity and sell it quickly.
Or you need the money urgently.

  • Know why it goes up, whether its based on rumor, or really have good news

Plan

If you really need sell some stock at specific time, plan it well

  • sleep early, set an alarm so you can get up
  • notice the unusual thing (wnw)
Make important decision carefully, seriously and take action
  • Take time to think the following questions:
  • Do I need money recently
  • Should I play extremely safe recently
  • Whether to sell (bad macro environment or bad news for the company)
  • Should I buy more, when?
  • How to sell

Do research before buying or selling

  • Check news if some unusual things happen
For new IPO stocks
  • be careful before earning dates, lockup expiry

Set limited sell even the stock is currently way below the desired price (for trash stock)

  • sometimes/some day, these stock will go up a lot for a short time

Prefer long term invest

  • much lower tax 15%, compared with income tax which may be 30% or higher Don’t manage(put) too much money on stock
  • Set a limit

Tax Season

Prepare tax as early as possible

  • So you know how much you owe

  • Leave enough more for the tax

  • Be very careful to put the money in stock at this time

    • Stock may(usually) fall during tax season
  • People may sell stock for tax (stock investor)

  • Don’t gamble in such short time period, if it fails, it would be difficult to pay the tax.

TODO

Wash-sale
  • How to make your loss really count as loss
  • Learn to analyze stock graph
  • know history, volatility, volume
Apps

Robinhood for trading

Related:
https://lifelongprogrammer.blogspot.com/2009/04/how-to-save-money.html

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)