BurningBright

  • Home

  • Tags

  • Categories

  • Archives

  • Search

Synchronization in compiling

Posted on 2017-06-07 | Edited on 2018-12-16 | In java

Synchronous in method

1
2
3
4
int i;
synchronized int fun() {
return ++i;
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
synchronized int fun();
Code:
0: aload_0
1: dup
// Field i:I
2: getfield #2
5: iconst_1
6: iadd
7: dup_x1
// Field i:I
8: putfield #2
11: ireturn

synchronized method is simply distinguished
in the run-time constant pool by the ACC_SYNCHRONIZED flag

Synchronous in block

1
2
3
4
5
6
int i;
void fun(Object obj, int j) {
synchronized(obj) {
i+=j;
}
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void fun(java.lang.Object, int);
Code:
0: aload_1
1: dup
2: astore_3
3: monitorenter
4: aload_0
5: dup
// Field i:I
6: getfield #2
9: iload_2
10: iadd
// Field i:I
11: putfield #2
14: aload_3
15: monitorexit
16: goto 26
19: astore 4
21: aload_3
22: monitorexit
23: aload 4
25: athrow
26: return
Exception table:
from to target type
4 16 19 any
19 23 19 any

monitorexit will be executed definitely
If not exit normally, #25 will throw exception

Throw try catch finally in compiling

Posted on 2017-06-06 | Edited on 2018-12-16 | In java

Throw an exception positively

1
2
3
4
void noZero(int i) throws Exception {
if(i == 0)
throw new Exception("no zero");
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void noZero(int) throws java.lang.Exception;
Code:
0: iload_1
1: ifne 14
// class java/lang/Exception
4: new #2
7: dup
// String no zero
8: ldc #3
// Method java/lang/Exception.
//"<init>":(Ljava/lang/String;)V
10: invokespecial #4
13: athrow
14: return

  1. if not equals pass the throw part
  2. if equals take constant string as param create a Exception.
  3. throw it to the caller[handler?].

Catch an exception

1
2
3
4
5
6
7
void e() {
try {
noZero(0);
} catch(ExcpA a) {}
catch(ExcpB b) {}
catch(Exception e) {}
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void e();
Code:
0: aload_0
1: iconst_0
// Method noZero:(I)V
2: invokevirtual #5
5: goto 17
8: astore_1
9: goto 17
12: astore_1
13: goto 17
16: astore_1
17: return
Exception table:
from to target type
0 5 8 Class HaveATry$ExcpA
0 5 12 Class HaveATry$ExcpB
0 5 16 Class java/lang/Exception

excption table
Every catcher have a chance to catch.

astore_1
Beginning of handler for excpSentry() store thrown value in local var 1.

Finally

1
2
3
4
5
6
7
8
9
int f() {
try { noZero(0); }
catch(Exception e) {
return 1;
}
finally {
return 2;
}
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int f();
Code:
0: aload_0
1: iconst_0
// Method noZero:(I)V
2: invokevirtual #5
5: iconst_2
6: ireturn
7: astore_1
8: iconst_1
9: istore_2
10: iconst_2
11: ireturn
12: astore_3
13: iconst_2
14: ireturn
Exception table:
from to target type
0 5 7 Class java/lang/Exception
0 5 12 any
7 10 12 any

5/6 copy instructions of 13/14.
7th is the Exception handler.
11th can’t reach.
what’s the object of istore_2/ istore_3 ???

istore_2 the catcher itself’s handler?
istore_3 the finaly’s handler comes from istore_2 ?

any mean in range instructions finished then goto target.

The final return handle by finally got 2

Oracle query table column comment

Posted on 2017-06-06 | Edited on 2018-12-16 | In db

Query Table

1
2
3
4
5
6
7
8
9
10
-- current user's table
select table_name from user_tables;

-- all user's table
select table_name from all_tables;

-- contain system's table
select table_name from dba_tables;
-- filter
select table_name from dba_tables where owner='用户名'

user_tables

Query Column

1
2
3
select * from user_tab_columns where Table_Name='xxx';
select * from all_tab_columns where Table_Name='xxx';
select * from dba_tab_columns where Table_Name='xxx';

user_tab_columns

Query Comment

1
2
3
4
5
6
7
8

select * from user_tab_comments;
select * from user_tab_comments t where t.COMMENTS like '%shit%' order by t.TABLE_NAME;

select * from user_col_comments;
select * from user_col_comments t where t.COLUMN_NAME='xxx';

select * from user_tab_comments t1, user_col_comments t2 where t1.TABLE_NAME = t2.TABLE_NAME and t2.COMMENTS like '%shit%';

user_tab_comments

user_col_comments

Arrays in compiling

Posted on 2017-06-05 | Edited on 2018-12-16 | In java

Simple array

1
2
3
4
5
6
7
8
void daily(){
int array[];
int size=10;
int value=123;
array = new int[size];
array[3] = value;
value = array[5];
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int[] daily();
Code:
0: bipush 10
2: istore_2
3: sipush 123
6: istore_3
7: iload_2
8: newarray int
10: astore_1
11: aload_1
12: iconst_3
13: iload_3
14: iastore
15: aload_1
16: iconst_5
17: iaload
18: istore_3
19: aload_1
20: areturn

[7] load variable 10 to create a new array.
[10] save array’s reference to array variable.
Is aload_x show that the primary type array variable is an object ?

Object array

1
2
3
4
5
6
Integer[] objectArray(){
Integer oArray[];
int count = 10;
oArray = new Integer[count];
oArray[0] = new Integer(7);
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java.lang.Integer[] objectArray();
Code:
0: bipush 10
2: istore_2
3: iload_2
// class java/lang/Integer
4: anewarray #2
7: astore_1
8: aload_1
9: iconst_0
// class java/lang/Integer
10: new #2
13: dup
14: bipush 7
// Method java/lang/Integer."<init>":(I)V
16: invokespecial #3
20: aload_1
21: areturn

Multianewarray

1
2
3
4
5
int[][][] create3darray() {
int matrix[][][];
matrix = new int[2][3][4];
return matrix;
}

javap

1
2
3
4
5
6
7
8
9
10
int[][][] create3darray();
Code:
0: iconst_2
1: iconst_3
2: iconst_4
// class "[[[I"
3: multianewarray #4, 3
7: astore_1
8: aload_1
9: areturn

Three constant used to create the 3 dimensional array.
Store to variable matrix, push to the top of stack.

Invoke in compiling

Posted on 2017-06-04 | Edited on 2018-12-16 | In java

Normal method

1
2
int calc(int i, int j) { return i+j; }
static int calc(int i, int j, int k) { return i+j+k; }

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int calc(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: ireturn

static int calc(int, int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: iload_2
4: iadd
5: ireturn

Nomal invoke

1
2
3
4
void call() {
calc(12, 13);
AboutInvoke.calc(12, 13, 14);
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void call();
Code:
0: aload_0
1: bipush 12
3: bipush 13
// Method calc:(II)I
5: invokevirtual #2
8: pop
9: bipush 12
11: bipush 13
13: bipush 14
// Method calc:(III)I
15: invokestatic #3
18: pop
19: return

Compiler generates symbolic references to
the methods(#2 #3) of an instance(Pointer?).

These symbols are store in run-time constant pool.

They are resolved at run-time to determine the actual method location.

Special invoke

Father

1
2
3
4
5
class Father{
int money = 10;
public int haveFun() { return getMoney(); }
private int getMoney() { return money; }
}

Son

1
2
3
class Son extends Father{
void havePunk() { super.haveFun(); }
}

javap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Father play
public int haveFun();
Code:
0: aload_0
// Method getMoney:()I
1: invokespecial #3
4: ireturn

// Son play
void havePunk();
Code:
0: aload_0
// Method Father.haveFun:()I
1: invokespecial #2
4: pop
5: return

invokespecial instruction always pass this to
the invoked method as its first argument.
As usual, it is received in local variable 0(aload_0).

  1. The method in super class call itself’s instance method.
    If the method is private , use invokespecial.

  2. If the sub class call super class’s instance method, all use invokespecial.

Arithmetic in compiling

Posted on 2017-06-01 | Edited on 2018-12-16 | In java

simple calc

1
2
3
int calc(int i, int j) {
return ((i + j - 1) & ~(j - 1));
}
  • variable 0 is this -> aload_0

javap -c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int calc(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: iconst_1
4: isub
5: iload_2
6: iconst_1
7: isub
8: iconst_m1
9: ixor
10: iand
11: ireturn
a b
[0] push i value into stack
[1] push j value into stack
[2] add; result push stack
[3] push constant 1 into stack
[4] subtract; push result
[5] j value push stack
[6] push constant 1 again
[7] subtract; push result
[8] push constant -1
[9] do XOR; push result
[10] do AND; push result
[11] return int
  • ~(j-1) transform into -1^(j-1)
    exclusive or, -1 bytes is all high bit.
    any int oxr with all high bit, the high bit will be low, low will be high.

Foundation in compiling

Posted on 2017-05-31 | Edited on 2018-12-16 | In java

tradition loop

1
2
3
4
5
6
public void spin() {
int i;
for(i=0; i<10; i++) {
;
}
}

In vm there many frame at any point,
but only the operand stack in currrent frame is active.

javap -c Spin.class

1
2
3
4
5
6
7
8
9
10
public void spin();
Code:
0: iconst_0
1: istore_1
2: iload_1
3: bipush 10
5: if_icmpge 14
8: iinc 1, 1
11: goto 2
14: return
a b
[0] push int constant 0
[1] store into local variable 1 (i=0)
[2] push local variable 1 (-> i)
[3] push int constant 10
[5] break if great or equals (i>=10)
[8] increment local variable 1 by 1 (i++)
[11] loop
[14] return void when done

Instructions

  • iconst_<i> push an int constant to operand stack
  • bipush immediate push int constant to operand stack
  • istore_<i> pops an int from the operand stack and stores it in local variable i
  • iload_<i> pushes the value in local variable i on to the operand stack

ZK queue

Posted on 2017-05-29 | Edited on 2018-12-16 | In db

Queue

  • Define

static public class Queue extends SyncPrimitive

class define in SyncPrimitive, it’s inner static class.

same as the previous article ZK barrier

  • Properties
    no properties

  • Constructor

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Queue(String address, String name) {
    super(address);
    this.root = name;
    // Create ZK node name
    if (zk != null) {
    try {
    Stat s = zk.exists(root, false);
    if (s == null) {
    zk.create(root, new byte[0],
    Ids.OPEN_ACL_UNSAFE,
    CreateMode.PERSISTENT);
    }
    } catch (KeeperException e) {
    ...
    } catch (InterruptedException e) {
    ...
    }
    }
    }

the constructor is not same as barrier, it no need common path.

  • push
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    boolean produce(int i) throws KeeperException, InterruptedException{
    ByteBuffer b = ByteBuffer.allocate(4);
    byte[] value;

    // Add child with value i
    b.putInt(i);
    value = b.array();
    zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
    CreateMode.PERSISTENT_SEQUENTIAL);
    return true;
    }

create a node with value.

  • pop
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    int consume() throws KeeperException, InterruptedException{
    int retvalue = -1;
    Stat stat = null;

    // Get the first element available
    while (true) {
    synchronized (mutex) {
    List<String> list = zk.getChildren(root, true);
    if (list.size() == 0) {
    System.out.println("Going to wait");
    mutex.wait();
    } else {
    String min = list.get(0).substring(7);
    for(String s : list){
    String tempValue = s.substring(7);
    min = tempValue.compareTo(min) < 0? tempValue: min;
    }

    System.out.println("Temporary value: " +
    root + "/element" + min);
    byte[] b = zk.getData(root +
    "/element" + min, false, stat);
    zk.delete(root + "/element" + min, 0);
    ByteBuffer buffer = ByteBuffer.wrap(b);
    retvalue = buffer.getInt();

    return retvalue;
    }
    }
    }
    }

Synchronously pop the minimum node name’s node.

Test queue

  • a producer

    1
    2
    3
    4
    5
    6
    7
    final String[] args = {"qTest", "192.168.3.11:2181", "7", "p"};
    new Thread(new Runnable(){
    @Override
    public void run() {
    queueTest(args);
    }
    }).start();
  • few customer

1
2
3
4
5
6
7
8
9
final String[] args = {"qTest", "192.168.3.11:2181", "3", "c"};
for (int i=0; i<Integer.valueOf(args[2]); i++) {
new Thread(new Runnable(){
@Override
public void run() {
queueTest(args);
}
}).start();
}

Thinking

Queue’s sequential rely on the node name’s sequential.
If nodes’ name sequential messed up, the queue is messed as well.

The queue rely on name form, so the data’s key must all be fetch from zookeeper.
If the key quantity is big, the I/O cost well be appreciable.

ZK barrier

Posted on 2017-05-27 | Edited on 2018-12-16 | In db

Zookeeper just like a cow boy, he know how to feed cattle.

Make a crowd of cattle move synchronized is not a easy thing.

To hold cattle, cowboy need barriers.

Barrier

  • Define

static public class Barrier extends SyncPrimitive

class define in SyncPrimitive, it’s inner static class.

It take it’s outter class as template, take outter class’s
static variable as common space.

  • Properties

    1
    2
    3
    4
    // max cow number
    int size;
    // the barrier's name
    String name;
  • Constructor

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    Barrier(String address, String root, int size) {
    // Create barrier node
    super(address);
    this.root = root;
    this.size = size;

    if (zk != null) {
    try {
    Stat s = zk.exists(root, false);
    if (s == null) {
    //path, no data, no privilege control, not temple node
    zk.create(root, new byte[0],
    Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    } catch (KeeperException e) {
    ...
    } catch (InterruptedException e) {
    ...
    }
    }

    // My node name
    try {
    name = new String(InetAddress.getLocalHost()
    .getCanonicalHostName().toString());
    } catch (UnknownHostException e) {
    ...
    }

    }

Used to create a barrier node in zookeeper.
super() used to create the zk client object.

  • In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
boolean enter() throws KeeperException, InterruptedException{
String path = zk.create(root + "/" + name, new byte[0],
Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
name = path.substring(path.lastIndexOf("/") + 1, path.length());
// keep check
while (true) {
synchronized (mutex) {
// get current cattles out
List<String> list = zk.getChildren(root, true);
System.out.println(Thread.currentThread()
.getName() + " " + list.size());
// enter when reach the threshold
if (list.size() < size) {
mutex.wait();
} else {
return true;
}
}
}
}

Once a cow, check the threshold.
If not reach, wait for more cow come, or enter barrier.
Loop checking, when wake up the threshold should reached.

CreateMode.EPHEMERAL_SEQUENTIAL like this _1, _2

  • Out
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    boolean leave() throws KeeperException, InterruptedException {
    zk.delete(root + "/" + name, 0);
    // keep check
    while (true) {
    synchronized (mutex) {
    List<String> list = zk.getChildren(root, true);
    if (list.size() > 0) {
    mutex.wait();
    } else {
    return true;
    }
    }
    }
    }

Once a cow, check the space empty or not.
If not empty, wait for more cow leave, or leave barrier.
Loop checking, when wake up the threshold should reached.

Test one barrier

  • In main
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    for (int i=0; i<3; i++) {
    new Thread(new Runnable(){
    @Override
    public void run() {
    barrierTest(args);
    }
    }).start();

    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

Simulate three cows.

  • Test method
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public static void barrierTest(String args[]) {
    Barrier b = new Barrier(args[1], "/b1", new Integer(args[2]));
    try{
    boolean flag = b.enter();
    System.out.println("Entered barrier: " +
    Thread.currentThread().getName());
    if(!flag) System.out.println("Error when entering the barrier");

    // simulate cow sleep
    Thread.sleep(3000);

    b.leave();
    } catch (KeeperException e){
    ...
    } catch (InterruptedException e){
    ...
    }
    System.out.println("Left barrier " +
    Thread.currentThread().getName());
    }

Thinking

This test’s synchroniz is base on cow notify another.
So it must make sure when cowboy[zookeeper] tiggered,
the transaction have bean done.

In other word, cattles should all enter then all out.
If run too fast, the last cattle can’t be notify, keep waiting.
If the number equal to the threshold, cattles can’t leave as well.

Hello ActiveMq

Posted on 2017-05-26 | Edited on 2018-12-16 | In db

Common

1
2
3
4
5
6
7
8
9
10
11
12
13
// connection's factory[modle]
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
JMSProducer.USERNAME,
JMSProducer.PASSWORD,
JMSProducer.BROKEURL);
// connection object[don't forget to close it]
Connection connection = connectionFactory.createConnection();
// begin
connection.start();
// communication with MQ[transaction modle, false in consumer]
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// MQ address object
Destination destination;

close connection in final

1
2
3
4
5
6
7
8
9
finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}

Producer-Consumer

producer

1
2
3
4
5
6
7
8
// the queue's name
destination = session.createQueue("HelloWorld");
// the producer object
MessageProducer messageProducer = session.createProducer(destination);
// message
TextMessage message = session.createTextMessage("Hello ActiveMQ");
// send
messageProducer.send(message);

consumer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// the queue's name
destination = session.createQueue("HelloWorld");
// the producer object
messageConsumer = session.createConsumer(destination);

while (true) {
// timeout if no response
TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
if(textMessage != null){
System.out.println("recieved:" + textMessage.getText());
}else {
break;
}
}

Publish-Subscribe

publish

1
2
3
4
5
6
7
8
9
10
// give a topic
Topic topic = session.createTopic("HelloTopic");
// message
TextMessage message = session.createTextMessage("ActiveMQ Topic");
// sender
MessageProducer producer = session.createProducer(topic);
// set tmp data
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// publish
producer.send(message);

subscribe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// got a topic
Topic topic = session.createTopic("HelloTopic");
// reciever
MessageConsumer consumer = session.createConsumer(topic);
// set a listener, deal callback
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage tmd = (TextMessage) message;
try {
System.out.println("Received message: " + tmd.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});

1…181920…29

Leon

282 posts
20 categories
58 tags
GitHub
Links
  • clock
  • typing-cn
  • mathjax
  • katex
  • cron
  • dos
  • keyboard
  • regex
  • sql
  • toy
© 2017 – 2024 Leon
Powered by Hexo v3.9.0
|
Theme – NexT.Muse v7.1.2