3, the multi-line Cheng of Java language programming method
The method that uses Java language to realize multi-line Cheng to use a program is very simple. What according to multi-line Cheng application process accedes or achieve a target is different and OK use two kinds of means: A kind of intercurrent moving object that is applied process accedes directly the line Cheng of Java kind Thread; Additionally one kind of means is to define intercurrent executive object to realize Runnable interface.
Successive Thread kind multi-line Cheng programming method
Thread kind be the definition in JDK be used at object of control line Cheng kind, in this kinds in enclosed the method that is used at undertaking line programmed control is made. See the code of give typical examples below:
[Code]//Consumer.java
Import Java.util. *;
Class Consumer Extends Thread
{
Int NTime;
String StrConsumer;
Public Consumer(int NTime, string StrConsumer)
{
This.nTime = NTime;
This.strConsumer = StrConsumer;
}
Public Void Run()
{
While(true)
{
Try
{
System.out.println("Consumer Name:" StrConsumer "n" );
Thread.sleep(nTime);
}
Catch(Exception E)
{
E.printStackTrace();
}
}
}
Static Public Void Main(String Args[] )
{
Consumer AConsumer = New Consumer (1000, "AConsumer" );
AConsumer.start();
Consumer BConsumer = New Consumer (2000, "BConsumer" );
BConsumer.start();
Consumer CConsumer = New Consumer (3000, "CConsumer " );
CConsumer.start();
}
} [/code]
From the program above code can see: The line Cheng in language of successive Java of Consumer of multi-line Cheng executive underground kind Thread and the example that established 3 Consumer target in Main method. When the Start method that calls object example, call Consumer automatically kind in Cheng of line of the object that start runs the Run method of the definition. The result that line Cheng runs is every removed NTime time the content of the StrConsumer of string member variable in printing an object example.
Can sum up a successive Thread kind multi-line Cheng programming the method is to make use a program kind successive Thread syncretic and intercurrent sex comes true to handle a process in this kinds Run method.
The multi-line Cheng that realizes Runnable interface programming method
The method that the Cheng of multi-line of additionally one kind of implementation that offers in Java language uses a program is multi-line Cheng object implementation Runnable interface and in this kinds in the Run method that the definition uses line Cheng with Yu Qi. It is OK that this kind of gain that defines way depends on multi-line Cheng using a target successive other object is not to must accede Thread kind, can increase the logic of kind of definition thereby.
Previous 1 2 34 5 6 7 8 Next