Uno scrittoio digitale …

17 luglio 2013

ThreadLocal

Archiviato in: java, oracle — Tag:, , , — admin @ 22:22

The javadoc says this:

“Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

If your application or (if you are talking about request threads) container uses a thread pool that means that threads don’t die. If necessary, you would need to deal with the thread locals yourself. The only clean way to do this is to call the ThreadLocal.remove() method.

There are two reasons you might want to clean up thread locals for threads in a thread pool:

to prevent memory (or hypothetically resource) leaks, or
to prevent accidental leakage of information from one request to another via thread locals.

Thread local memory leaks should not normally be a major issue with bounded thread pools since any thread locals are likely to get overwritten eventually; i.e. when the thread is reused. However, if you make the mistake of creating a new ThreadLocal instances over and over again (instead of using a static variable to hold a singleton instance), the thread local values won’t get overwritten, and will accumulate in each thread’s threadlocals map. This could result in a serious leak.

private void cleanThreadLocals() {
        try {
            // Get a reference to the thread locals table of the current thread
            Thread thread = Thread.currentThread();
            Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
            threadLocalsField.setAccessible(true);
            Object threadLocalTable = threadLocalsField.get(thread);

            // Get a reference to the array holding the thread local variables inside the
            // ThreadLocalMap of the current thread
            Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
            Field tableField = threadLocalMapClass.getDeclaredField("table");
            tableField.setAccessible(true);
            Object table = tableField.get(threadLocalTable);

            // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object
            // is a reference to the actual ThreadLocal variable
            Field referentField = Reference.class.getDeclaredField("referent");
            referentField.setAccessible(true);

            for (int i=0; i < Array.getLength(table); i++) {
                // Each entry in the table array of ThreadLocalMap is an Entry object
                // representing the thread local reference and its value
                Object entry = Array.get(table, i);
                if (entry != null) {
                    // Get a reference to the thread local object and remove it from the table
                    ThreadLocal threadLocal = (ThreadLocal)referentField.get(entry);
                    threadLocal.remove();
                }
            }
        } catch(Exception e) {
            // We will tolerate an exception here and just log it
            throw new IllegalStateException(e);
        }
    }

A good place to do such cleanup is ServletRequestListener.requestDestroyed().

29 aprile 2012

Python script

Archiviato in: oracle — Tag:, , , , — admin @ 14:26

Per il download di tutti i pdf collegati alla pagina del prof. cazzola per il corso di PA:


import re
import os
import base64
import urllib.request

baseUrl = "http://cazzola.dico.unimi.it/"
username = ""
password = ""

if __name__ == "__main__":
    fd = open('page.html')

    opener = urllib.request.build_opener()
    base64string = base64.encodestring(bytes('{0}:{1}'.format(username, password), "utf-8"))[:-1]
    opener.addheaders = [('Authorization', "Basic {0}".format(base64string.decode()))]

    for line in fd.readlines():
        paths = re.findall("[a-zA-Z0-9/\-%\(\).&;]+pdf", line)

        if (len(paths) > 0):
            url = paths[0].replace("&amp;", "&")
            fl = opener.open(baseUrl + url)
            name = url.split("/")[-1].replace("%20", " ")

            pdf = open("output" + os.sep + name, "wb")
            pdf.writelines(fl.readlines())
            pdf.close()

            print("downloaded ... " + name)

Nota: username e password devono essere richiesti direttamente a lui

13 dicembre 2011

IE6

Archiviato in: oracle — Tag:, — admin @ 22:45

help
:D

9 ottobre 2011

Java Observer Pattern

Archiviato in: code, java, oracle — Tag:, — admin @ 12:58

In relazione al pattern, java fornisce delle API già pronte all’uso:

observable
observer

Mitico :D

20 giugno 2011

Oracle Number

Archiviato in: java, oracle — Tag:, , — admin @ 12:04

Da documentazione Oracle

The NUMBER(p,s) type takes two arguments; precision and scale.
The precision of a number its number of significant decimal digits, and its scale is the number of digits after the decimal point.

NUMBER(7,0) … Java Integer
NUMBER(38,0) … Java BigDecimal

Funziona con WordPress