Updates from February, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 3:46 pm on February 25, 2010 Permalink |
    Tags: , , , ,   

    Today’s read: The seven deadly sins of JavaScript implementation 

    A Smashing Magazine article on the common “sins” of JavaScript implementation by Christian Heilmann.

    Using JavaScript has become increasingly easy over the last few years. Whereas back in the day we needed to know the quirks of every browser, now many libraries such as jQuery, YUI, Dojo and MooTools allow someone who doesn’t even know JavaScript to spruce up boring HTML documents with impressive and shiny effects. By piggy-backing on the CSS selector engine, we have moved away from the complexity and inconsistencies of the DOM and made things much easier.

    If you look at some of the code that has been released, though, we do seem to have taken a step backwards. In gaining easier access, we also became a bit sloppy with our code. Finding clearly structured, easy-to-maintain jQuery code is quite tough, which is why many plug-ins do the same thing. Writing one yourself is faster than trying to fathom what other developers have done.

    Read the articleFollow the author in twitter !

     
  • Subinkrishna Gopi 2:45 pm on February 24, 2010 Permalink |
    Tags: , , , google voice,   

    Funny: How Google Voice can ruin your relationships ! 


    This is seriously funny! Funnily serious too. I read it in gizmodo.com.

    Read the article here:
    http://gizmodo.com/5478789/this-is-how-google-voice-will-ruin-your-relationships

     
  • Subinkrishna Gopi 11:37 am on February 23, 2010 Permalink |
    Tags: , help, htmlunit, , , , memory leak, ,   

    HTMLUnit issue 

    We are using HTMLUnit in one of our modules and we use it to crawl through the webpages. But we are facing serious memory leak issues with HTMLUnit. We are unable to kill the JavaScript threads even after the functionality is completed. All my search to fix this issue ended up in one solution,

    webClient.closeAllWindows();
    

    But even this did not help us in solving the leak. The method call claims to kill all the running JavaScript threads associated with the WebClient though! If anyone can help us in fixing this, can share their thoughts here. I will update this post as soon as we have a solution.

    Thank you.

     
    • kkperf 11:28 pm on April 9, 2010 Permalink

      We seem to be running into a similar issue.
      We noticed “leaking windows” – always with a scriptable object tied to it. Here is what we did to confirm this (in addition to heap dump analysis)

      System.out.println(“windows size before closeAllWindows: ” + windows.size());
      webClient.closeAllWindows();
      System.out.println(“windows size after closeAllWindows: ” + windows.size());

      List windowsAfterClose = webClient.getWebWindows();
      System.out.println(“windowsAfterClose size: ” + windowsAfterClose.size());
      for (WebWindow ww : windowsAfterClose) {
      System.out.println(“Window name: ” + ww.getName());

      This shows that there is 1 window with no name still open.

      Trying to figure out whats going on. I’m going to perhaps end up logging a bug with htmlunit.
      Let me know if you have found a solution to the problem.
      Thanks
      -KK

    • kswenson 2:53 am on August 3, 2010 Permalink

      I have this exact problem.

      I have iterated through all the of the windows, gotten the ThreadManager, and interrupted all treads managed by those windows.

      Then I close all windows.

      But background JavaScript is still running. Every few seconds it creates a new thread and executes something.

      There is a class JavaScriptBackgroundJob which is running, but this class is not mentioned anywhere in the JavaDoc shipped with the library.

      I would appreciate help from anyone who can tell me how to COMPLETELY shut down all htmlunit processing.

    • Subinkrishna G 12:44 pm on August 4, 2010 Permalink

      We still dont have any solution 😦 I will update this post as soon as I have one.

  • Subinkrishna Gopi 3:12 pm on February 19, 2010 Permalink |
    Tags: , executor, , , , thread pool, threadpoolexecutor   

    ThreadPoolExecutor – basics 

    ThreadPoolExecutor is an implementation of the ExecutorService, which can be used to execute the submitted tasks using the available Thread pool. Read the Sun Java API docs for more information.

    The buzz words

    1. Core & max pool size
    This is the size of the core (minimam) and maximum possible thread that need to be in the thread pool every time. We can even specify the keep-alive time limit for the threads.

    2. Work queue
    Work queue is a BlockingQueue, which is used to hold the work/task which is being sent to the executor when all the threads in the pool are busy executing the tasks. Read more about the BlockingQueue in Sun Java API docs.

    3. RejectedExecutionHandler
    So we have a limited number of threads in the pool to execute the tasks and a fixed sized work queue to hold the additional tasks. But what if the work queue overflows! RejectedExecutionHandler can help us in such situations. Read the Sun Java API docs for more.

    Some sample code

    Initializing the ThreadPoolExecutor and sending the task. Here we’ve defined a worksQueue with size 2, core & max pool size is 3 and the threads will expire after 10 seconds. We are also starting a daemon thread to monitor the executor status.

    BlockingQueue<Runnable> worksQueue = new ArrayBlockingQueue<Runnable>(2);
    RejectedExecutionHandler executionHandler = new MyRejectedExecutionHandelerImpl();
    
    // Create the ThreadPoolExecutor
    ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 10,
            TimeUnit.SECONDS, worksQueue, executionHandler);
    executor.allowCoreThreadTimeOut(true);
    
    // Starting the monitor thread as a daemon
    Thread monitor = new Thread(new MyMonitorThread(executor));
    monitor.setDaemon(true);
    monitor.start();
    
    // Adding the tasks
    executor.execute(new MyWork("1"));
    executor.execute(new MyWork("2"));
    executor.execute(new MyWork("3"));
    executor.execute(new MyWork("4"));
    executor.execute(new MyWork("5"));
    executor.execute(new MyWork("6"));
    executor.execute(new MyWork("7"));
    executor.execute(new MyWork("8"));
    
    try
    {
        Thread.sleep(30000);
        executor.execute(new MyWork("9"));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    

    RejectedExecutionHandler implementation.

    /**
     * The custom {@link RejectedExecutionHandler} to handle the rejected
     * tasks / {@link Runnable}
     */
    public class MyRejectedExecutionHandelerImpl
    implements RejectedExecutionHandler
    {
        @Override
        public void rejectedExecution(Runnable runnable,
                ThreadPoolExecutor executor)
        {
            System.out.println(runnable.toString() + " : I've been rejected ! ");
        }
    }
    

    My task (implements Runnable).

    /**
     * My {@link Runnable} class. Represents a task which need to be executed.
     */
    public class MyWork implements Runnable
    {
        String name;
    
        public MyWork(String name)
        {
            this.name = name;
        }
    
        @Override
        public void run()
        {
            System.out.println(this.name + " : I'm running ! ");
    
            try
            {
                Thread.sleep(5000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
    
            System.out.println(this.name + " : I'm done ! ");
        }
    
        @Override
        public String toString()
        {
            return (this.name);
        }
    }
    

    And the monitor thread.

    /**
     * My monitor thread. To monitor the status of {@link ThreadPoolExecutor}
     * and its status.
     */
    public class MyMonitorThread implements Runnable
    {
        ThreadPoolExecutor executor;
    
        public MyMonitorThread(ThreadPoolExecutor executor)
        {
            this.executor = executor;
        }
    
        @Override
        public void run()
        {
            try
            {
                do
                {
                    System.out.println(
                        String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                            this.executor.getPoolSize(),
                            this.executor.getCorePoolSize(),
                            this.executor.getActiveCount(),
                            this.executor.getCompletedTaskCount(),
                            this.executor.getTaskCount(),
                            this.executor.isShutdown(),
                            this.executor.isTerminated()));
                    Thread.sleep(3000);
                }
                while (true);
            }
            catch (Exception e)
            {
            }
        }
    }
    

    So we have all necessary code blocks in place. If we execute the above code we may get something like this.

    1 : I'm running !
    2 : I'm running !
    3 : I'm running !
    6 : I've been rejected !
    7 : I've been rejected !
    8 : I've been rejected !
    [monitor] [0/3] Active: 1, Completed: 0, Task: 1, isShutdown: false, isTerminated: false
    [monitor] [3/3] Active: 3, Completed: 0, Task: 5, isShutdown: false, isTerminated: false
    1 : I'm done !
    4 : I'm running !
    2 : I'm done !
    5 : I'm running !
    3 : I'm done !
    [monitor] [3/3] Active: 2, Completed: 3, Task: 5, isShutdown: false, isTerminated: false
    [monitor] [3/3] Active: 2, Completed: 3, Task: 5, isShutdown: false, isTerminated: false
    4 : I'm done !
    5 : I'm done !
    [monitor] [3/3] Active: 0, Completed: 5, Task: 5, isShutdown: false, isTerminated: false
    [monitor] [2/3] Active: 0, Completed: 5, Task: 5, isShutdown: false, isTerminated: false
    [monitor] [2/3] Active: 0, Completed: 5, Task: 5, isShutdown: false, isTerminated: false
    [monitor] [0/3] Active: 0, Completed: 5, Task: 5, isShutdown: false, isTerminated: false
    [monitor] [0/3] Active: 0, Completed: 5, Task: 5, isShutdown: false, isTerminated: false
    [monitor] [0/3] Active: 0, Completed: 5, Task: 5, isShutdown: false, isTerminated: false
    9 : I'm running !
    [monitor] [1/3] Active: 1, Completed: 5, Task: 6, isShutdown: false, isTerminated: false
    [monitor] [1/3] Active: 1, Completed: 5, Task: 6, isShutdown: false, isTerminated: false
    9 : I'm done !
    [monitor] [1/3] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
    [monitor] [1/3] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
    [monitor] [1/3] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
    

     
    • nguyenhuuquan 9:28 pm on April 29, 2013 Permalink

      thanks, it’s helpful. I have a question: if a task take a long time to execute, how to terminate it?

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel