Wednesday, February 20, 2008

Taking the next step


Before we proceed further,one must understand the essence of Beans. To better understand its significance,consider the following. Resistors,capacitors and inductors form the building blocks of a simple electronic circuit. All these components can be reused in complex/integrated circuits which probably yield more efficiency. This is because the functionality of every component is understood and it does not change with the system. So,every aspect of the component's behaviour can be documented. Unfortunately,until recently "write-once,run-anywhere" paradigm has not been highly successful in achieving the benefits of reusability and portability. Large applications grow high in complexity and it becomes very hard to enhance. Part of the problem is that ,until recently,there has not been a standard,portable way to write software component. This is where the role of a bean comes into play. As per standard definition, a bean is a "software component that has been designed to be reusable in a variety of environments". It may perform a simple operation such as spell check in a document or a complex one like forecasting the performance of a stock.


Netbeans is one such platform which also aids software development. In netbeans,it is important to realise that the platform recognizes and evaluates strictly in terms of a 'project'.


Time to learn:
Of the Java SE applications of netbeans,there is an application called 'Java project with existing sources wizard'. Click on File->New project. Click on java and choose the 'Java project with existing sources wizard'. Then,choose a project name and a project folder and click next. When adding the source package folder,add the folder which contains your java programs. [In a windows system,by default,all these java programs are present in C:\Program files\Java\jdk(version)\bin]Finally,click finish and your project is created.
NOTE: If you include this folder,then the whole folder becomes a project and your individual programs gain the status of a file.

If you have followed all the steps I have mentioned above,then its first part accomplished


Now,you can run any file present in your project. To open your file,click on your project folder which will open all the files tored in it. In my example,I am going to run a program which is stored as "Accurate.java" in my systen. To illustrate this,I am now going to run a file in my project. The screenshot is as shown below.







The program I have shown is a basic multithread program. Its code is



class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: " + t);
t.start();
}
public void run()
{
try
{
for(int i=15;i>0;i--)
{
System.out.println(name + ":" +i);
Thread.sleep(200);
}
} catch (InterruptedException e)
{
System.out.println(name + "interrupted");
}
System.out.println(name + "exiting");
}
}
class Accurate
{
public static void main(String args[])
{
NewThread ob1= new NewThread("One");
NewThread ob2= new NewThread("Two");
try{
Thread.sleep(1000);
ob1.t.suspend();
System.out.println("Suspending thread one");
Thread.sleep(1000);
ob1.t.resume();
System.out.println("Resuming thread one");
Thread.sleep(1000);
ob2.t.suspend();
System.out.println("Suspending thread two");
Thread.sleep(1000);
ob2.t.resume();
System.out.println("Resuming thread one");
} catch (InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread exiting");
}
}



To run this file,we click on Build->"Complile Accurate.java" (F9). After succesful compilation,we can run the program by clicking Run->Run file->Run "Accurate.java" (or Shift+F6). After this,we can observe the output which is




New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
One:15
Two:15
One:14
Two:14
One:13
Two:13
One:12
Two:12
One:11
Two:11
Suspending thread one
Two:10
Two:9
Two:8
Two:7
Two:6
Resuming thread one
One:10
Two:5
One:9
Two:4
One:8
Two:3
One:7
Two:2
One:6
Two:1
Suspending thread two
One:5
One:4
One:3
One:2
One:1
Resuming thread one
Twoexiting
Main thread exiting
Oneexiting

No comments: