Sunday, June 17, 2012
Thursday, March 29, 2012
Wednesday, March 28, 2012
Lock and condition java 5 concurrent api
Thursday, March 22, 2012
Google question
Wednesday, March 21, 2012
Friday, March 16, 2012
Python ---head first
except IOError as err:
Thursday, March 15, 2012
Just Spring reading notes
Tuesday, March 13, 2012
Web container
Sunday, March 11, 2012
Java IO
InputStream
and OutputStream
.FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt");
Closing a stream when it's no longer needed is very important — so important thatCopyBytes
uses afinally
block to guarantee that both streams will be closed even if an error occurs. This practice helps avoid serious resource leaks.
all other stream types are built on byte streams.
--------------
All character stream classes are descended fromReader
andWriter
.
Reader.read();
Writer.write(c)
Line-Oriented IO has to use bufferedReader and BufferedWriter
--
Above are unbuffered IO,This means each read or write request is handled directly by the underlying OS.
to reduce overhead, the Java platform implements buffered I/O streams.
using wrapping approach
inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));Decorator Design pattern!!!
-----
Objects of type Scanner
are useful for breaking down formatted input into tokens and translating individual tokens according to their data type.
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt"))); while (s.hasNext()) { System.out.println(s.next()); }s.useDelimiter(",\\s*");s.hasNextDouble();---Stream objects that implement formatting are instances of eitherPrintWriter
, a character stream class, orPrintStream
, a byte stream class.System.out
is PrintStream
Decorator Pattern
HOW PL/SQL GETS EXECUTED
the PL/SQL engine on the server where it is compiled. The named PL/SQL
block is compiled only at the time of its creation, or if it has been
changed. The compilation process includes syntax checking, binding,
and p-code generation.
Oracle guarantees a read-consistent view
of the data. Until that point, all data that has been inserted or updated
will be held in memory and only be available to the current user. The
rows that have been changed will be locked by the current user and will
not be available for updating to other users until the locks have been re-leased.
Saturday, March 10, 2012
Difference between web server and app server
A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.
Wednesday, March 7, 2012
Monday, March 5, 2012
Servlet basic
Tuesday, February 14, 2012
Restful Web Services (Note from the book by the same name)
Sunday, February 12, 2012
HTTP and more
Saturday, February 11, 2012
Web Basics
Wednesday, February 8, 2012
线程之间怎么通讯?什么是critical section/semaphore/mutax,区别?
网上找到这么一段话,
how do 2 threads communicate?
Basically via memory i.e. member fields of a class. Any thread can write into a single field and let any other thread read its value.
But if you need to make sure that any value written into the field will also be found/read by another thread, you need to put the statements accessing your communication field into blocks
synchronized( lockObj ){ commField= ...; }
The lockObj is a central object (also called semaphore or mutex - for mutual exclusion ) you should choose carefully so that it can be accessed from all your classes and very early: think of an instance of java.lang.Class : that's a foolproof singleton.
I want to discourage you from using the "fast and easy" synchronized qualifier for methods. This way you end up with a mess of Locks and creating deadlocks!
In concurrent programming a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will have to wait a fixed time to enter it (aka bounded waiting). Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use.
In computer science, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment.
In many cases a mutex has a concept of an "owner": the process which locked the mutex is the only process allowed to unlock it. In contrast, semaphores generally do not have this restriction, something the producer-consumer example above depends upon.
Thursday, February 2, 2012
heap vs stack
What is stack?
The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated for automatic variables within functions.
Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.
What is heap?
On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.
The stack is much faster than the heap but also smaller and
Semaphores in JDK 1.5
Wednesday, February 1, 2012
Nth Highest Salary in Oracle
First things first: The question is ambiguous!
Let’s say this is your data:
Name | Salary |
KING | 5000 |
FORD | 3000 |
SCOTT | 3000 |
JONES | 2975 |
BLAKE | 2850 |
CLARK | 2850 |
ALLEN | 1600 |
Who is second – FORD or SCOTT or both?
What will you say about JONES’s salary – is it the 3rd highest salary, or the 4th highest?
If you are looking for the set of people earning the Nth highest salary, with no gaps in case of ties, then JONES should be ranked 3rd, after KING [5000, 1st], followed by FORD and SCOTT [both 3000, 2nd].
If you are looking for exact ranks with gaps if there are ties, then JONES is the 4th highest paid employee, as there are 3 people earning more than him – KING, FORD and SCOTT. In this system of ranking, FORD and SCOTT are 2nd jointly and no employee is 3rd.
This is how your ranks will look, in the 2 cases:
|
Scenario 1: No gaps in case of ties Scenario 2: Gaps in case of ties
Once you have your question sorted out -
(a) Set of people earning the Nth highest salary, with continuous ranks if there are ties, OR
(b) Set of people earning the Nth highest salary, with skipped rank numbers if there are ties
Then you can proceed to writing the queries.
Scenario 1: DENSE_RANK () for Nth highest row, no gaps in case of ties
The analytic function dense_rank() will rank the rows with no gaps in ranking sequence if there are ties.
The ranks are calculated as: SQL> select ename
2 ,sal 3 ,dense_rank() over (order by sal desc) ranking 4 from emp; ENAME SAL RANKING ---------- ---------- ---------- KING 5000 1 FORD 3000 2 SCOTT 3000 2 JONES 2975 3 CLARK 2850 4 BLAKE 2850 4 ALLEN 1600 5
Wrap a filter around and pick out the Nth highest salary, say the 4th highest salary.
SQL> select *
2 from 3 ( 4 select ename 5 ,sal 6 ,dense_rank() over (order by sal desc) ranking 7 from emp 8 ) 9 where ranking = 4 -- Replace 4 with any value of N 10 / ENAME SAL RANKING ---------- ---------- ---------- BLAKE 2850 4 CLARK 2850 4
The 4th position has a tie between BLAKE and CLARK.
Scenario 2: RANK () for Nth highest row, gaps in case of ties
The analytic function rank() will rank the rows with gaps in ranking sequence if there are ties.
The ranks are calculated as:
SQL> select ename
2 ,sal 3 ,rank() over (order by sal desc) ranking 4 from emp; ENAME SAL RANKING ---------- ---------- ---------- KING 5000 1 FORD 3000 2 SCOTT 3000 2 JONES 2975 4 CLARK 2850 5 BLAKE 2850 5 ALLEN 1600 7 TURNER 1500 8
Wrap a filter around and pick out the Nth highest salary, say the 4th highest salary.
SQL> select *
2 from 3 ( 4 select ename 5 ,sal 6 ,rank() over (order by sal desc) ranking 7 from emp 8 ) 9 where ranking = 4 -- Replace 4 with any value of N 10 / ENAME SAL RANKING ---------- ---------- ---------- JONES 2975 4
A different answer from the previous query, as there is no rank 3 because of the tied 2nd place.
Closing Notes
The requirement to “find Nth highest row” is incomplete, until the following questions are also answered:
- Can the result match more than one value? If not, on what basis should the one record be chosen if there is a tie?
- How should the subsequent records be ranked in case of ties – contiguously or with gaps?
Depending on the answer for (2), DENSE_RANK (for contiguous) or RANK (for gaps) can be used. Depending on the answer for (1), extra filter criteria can be applied to the SQL.
There are other approaches for calculating the Nth highest row, too. The next is a non-analytic approach, which works the same way as the RANK query (gaps for ties).
SQL> select ename 2 , sal 3 from emp a 4 where 3 = ( select count(*) -- Replace 3 with any value of (N - 1) 5 from emp b 6 where b.sal > a.sal) 7 / ENAME SAL ---------- ---------- JONES 2975
However, tests have shown the analytics approach to be more efficient than the non-analytics one for Nth highest or Top-N type of queries.