How to do it...

Follow these steps to implement the example:

  1. Create a class named Task that extends the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private ReentrantLock attribute named lock:
        private ReentrantLock lock;
  1. Implement a constructor of the class:
        public Task(ReentrantLock lock) { 
this.lock=lock;
}
  1. Implement the run() method. Get control of the lock, put the thread to sleep for 2 seconds, and free the lock:
        @Override 
public void run() {
lock.lock();
try {
TimeUnit.SECONDS.sleep(1);
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Create the main class of the example by creating a class named Main with a main() method:
        public class Main { 
public static void main(String[] args) {
  1. Declare and create a ReentrantLock object named lock:
        ReentrantLock lock=new ReentrantLock();
  1. Create 10 Task objects and 10 threads to execute the tasks. Start the threads by calling the run() method:
          for (int i=0; i<10; i++) { 
Task task=new Task(lock);
Thread thread=new Thread(task);
thread.run();
}
}
  1. Export the project as a .jar file. Call it recipe7.jar. Use the menu option of your IDE or the javac and .jar commands to compile and compress your application.
  2. Start the FindBugs standalone application by running the findbugs.bat command in Windows or the findbugs.sh command in Linux.
  1. Create a new project by clicking on the New Project option under the File menu in the menu bar:
  1. The FindBugs application shows a window to configure the project. In the Project name field, type Recipe07. In the Classpath for analysis field (jar, ear, war, zip, or directory), add the .jar file with the project. In the Source directories field (optional; classes used when browsing found bugs), add the directory with the source code of the example. Refer to the following screenshot:
  1. Click on the Analyze button to create the new project and analyze its code.
  2. The FindBugs application shows the result of the analysis of the code. In this case, it has found two bugs.
  3. Click on one of the bugs and you'll see the source code of the bug on the right-hand side panel and the description of the bug in the panel at the bottom of the screen.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.216.7