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

Java (161) Lucene-Solr (112) Interview (64) All (58) J2SE (53) Algorithm (45) Soft Skills (39) Eclipse (33) Code Example (31) JavaScript (23) Linux (22) Spring (22) Tools (22) Windows (22) Web Development (20) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) Troubleshooting (14) J2EE (13) Network (13) Tips (12) PowerShell (11) Chrome (10) Problem Solving (10) Design (9) How to (9) Learning code (9) Performance (9) Security (9) UIMA (9) html (9) Http Client (8) Maven (8) bat (8) blogger (8) Big Data (7) Database (7) Google (7) Guava (7) JSON (7) Shell (7) System Design (7) ANT (6) Coding Skills (6) Lesson Learned (6) Programmer Skills (6) Scala (6) css (6) Algorithm Series (5) Cache (5) Continuous Integration (5) IDE (5) adsense (5) xml (5) AIX (4) Become a Better You (4) Code Quality (4) Concurrency (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Life (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Review (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Distributed (3) Dynamic Languages (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Mac (3) Python (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Fiddler (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Logging (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Firefox (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) Invest (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts