跳至主要内容

博文

目前显示的是 十二月, 2016的博文

Synchronize on Method Call

This week, we are going to talk about a problem we may not notice in common concurrency code but may bite you later. Synchronize on Method Call Quick Question See the following code: thread one will start first, then thread two. So, will the first element of list printed? Or deeper, can second thread enter that synchronized block? List list; // thread one synchronized (list) { while ( true ) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } } } // thread two synchronized (list.get( 0 )) { System.out.println(list.get( 0 )); } Answer The answer is yes. Do you get it? let’s analyse what the essence of this problem. First, we can simply find the the lock of list is always hold by thread one for its infinite loop. So, whether the second thread can enter that synchronized block depends on whether we need to get the lock of list. Now, we need to understand: whether we get option one or option two wh...

Concurrency: How to Wait

Basic wait When we want to coordinate multiple threads to run in order, there are many useful synchronizer classes in Java library for us to use, like Semaphore , CyclicBarrier , CountDownLatch etc. A common usage of synchronizer class is consumer and producer problem. If we decide to use some basic method from internal lock, we can do it like following: // thread one while (ready) { obj.wait(); } // thread two ready = true ; obj.notify(); // or, depends on situation, obj.notifyAll(); Or we can use explicit lock: Lock & Condition to do similar thing. condition .await () ; condition .signal () ; And the above code is more a practice than a real usage. If you really need to write a producer and consumer solution, BlockingQueue may be more convenient to do it. waitForAny When we get familiar with the common use of wait, we are going to implement waitForAny utility function based on other library code of java. Definition As what we always do, we...

How to Kill

I always use some new release of Linux distribution, so some software froze from time to time. In order to restart it, kill command can be very handy to use for it so fast and saves lot of time compared with UI prompt. So today, we learn how to kill effectively. Kill First, we should understand how to use kill . But we are surprised to find the man kill only tell us the common format: $ kill [signal or option] PID (s) But what’s the signal? Which one should I choose? Signal is a kind of inter-process communication which we can take it as a message sent between processes. And we usually use the following three signals: Signal Name Signal Value Behaviour SIGHUP 1 Hangup SIGKILL 9 Kill Signal SIGTERM 15 Terminate Some kinds of signal can be ignored, like SIGTERM ; but some can’t, like SIGKILL . So what we commonly use is the following: kill - 9 pid Find the PID...

Hadoop Beginner Guide

In this blog, we will learn some basic concepts and basic API about Hadoop. Overview of Hadoop Hadoop is an Apache open source framework written in java that allows distributed processing of ‘Big Data’ across clusters of computers using simple programming models. What Is Big Data? Hadoop is the software to handle big data, but what is big data? Big Data has following features: Big volume Big velocity Big variety Structure of Hadoop The following is some components of Hadoop: A distributed file system called Hadoop Distributed File System (HDFS) to store data A framework and API for building and running MapReduce jobs to handle data Hadoop common: utilities Hadoop YARN: job scheduling and cluster resource management Range and Aim In order to achieve better effect of learning, we should have a clear aim and detailed road map. So we first define the range we will learning: basic concepts and architecture of hadoop basic API Aim: understand ...