| Multithread programming in java |
|
|
|
Those, who know Java programming well, also shure know, that Java is thread-oriented programming language. And any Java programmer can easily create some threads to calculate some math equestion or something like this. But for novices it would be interesting to get known something about java threads and how to create them. So, in some words, thread it's and independant part of a running program, which has it's own resources and tasks. In fact, java program is the thread itself. This thread can generate other threads and other threads can generate their own threads too. And so on and so on... This java future allows to easily create java-based agent software. What is agent software, can you ask. I'll try to explain that in few fords. So, agent software is software which has following possibilities: It can interact with user and get some task from he or she; It can interact with environement and get knowlege from it; It can accumulate knowleges; It can produce rules on the base of accumulated knowledges; Also some additional options are possible, but those are the main. And how can I create such an agent, will you ask. It's not so hard. Just remember, that this software should perform some task from user and perform them without errors. For to begin, lets discuss how to create threads in Java. For those purposes in Java there are two possibilities of creating agents. The first is that you can create your own class (for example, public class MyClass) and then extend Thread class within your class (public class MyClass extends Thread). This was the 1-st way to create thread oriented class (but not new thread itself). The 2-nd way seems reasonable when your class extends some other class, so you can not extend an class any more. In this case just implement interface Runnable (public class MyClass extends OtherClass implements Runnable). So, thread-oriented calss is ready. Then lets create new thread. This is very simple. Just create a function (method), caled run(). Example 1: public class MyClass extends Thread { public void run() { System.out.println("Thread is created"); } } So now you created function, which contains the body of the thread. Just realise, that all which is written insite of the run() function is a body of a thread. But the run() method will be called only once. So, as is written in the example 1, words "Thread is created" will be printed once on the screen and then thread will terminates. But we might want the new thread to work a bit longer than once. In this case lets make an andless loop for the thread. Example 2: public class MyClass extends Thread { public void run() { while(true) { System.out.println("Thread is created"); } } } This example will print "Thread is running" permanently. But, as you see, there is no main method in this program. so, let's create it. Note, that to start a thread from the method main, programmer should call either run() or start() method. This is caused because the run() method is called from the start() method. In fact, start() function contains the only call to the run() function. So, let's see what we have now: Example 3: public class MyClass extends Thread { public static void main(String args[]) { MyClass mc = new MyClass() mc.run(); } public void run() { while(true) { System.out.println("Thread is created"); } } } Now our small program is ready for compilation. Just type javac MyClass.java and then run the program by typing java MyClass. Unfortunatelly you can stop this program only with Ctrl-c keys. Note, that for to start new thread we had to create new object of the MyClass instance. This is done because in Java we have no possibility to call non- static objects from the static contents. And main() method is static, as you can see. So we had to create an object of the MyClass inctance and only then run a new thread. |



