Some notes on the Thread and Runnable

Here is a simple multi-threaded approach, each thread can perform their own run method alone.

package hello.test;


// on some of the Thread and Runnable
public class the Test {
// create a thread, there are two ways, one is to declare a class to be thread subclasses. This subclass should override the run method of class thread. You may be allocated and then start an instance of a subclass.
{PrimeThread the extends the Thread class
// pass may need to use some of the parameters come
long minPrime;

PrimeThread (long minPrime) {
this.minPrime = minPrime;
}

RUN void public () {
// want to use this particular thread doing

}
}

// create a thread and start it running
public void Test () {
PrimeThread PrimeThread new new = P (143);
p.start ();
}

// Another way to create a thread is to declare achieve operational interface class. The class then implements the run method. You can then assign the class instance, passed as a parameter when creating a thread, and then start.

static class PrimeRun implements Runnable {
long minPrime;

PrimeRun (long minPrime) {
this.minPrime = minPrime;
}

RUN void public () {
for (int I = 0; I <minPrime; I ++) {
System.out.println (I);
}
}
}

// create a thread and start it running
public static void test1 () {
PrimeRun P1 PrimeRun new new = (10);
new new the Thread (P1) .start ();
}

// each thread has a name for identification. Multiple threads can have the same name. If the name is not specified when a thread is created, the switch will generate a new name. Unless otherwise noted, passing null parameter to the constructor or method in this class will cause an NullPointerException.
// may be recycled to write a multi-threaded approach
public static void main (String [] args) {
the try {
for (int I = 0; I <. 4; I ++) {
test1 ();
}
} the catch (Exception E) {
the System .out.println (E);
}
}
}

 

Both methods have their own advantages and disadvantages:

The difference in the source code
inheritance Thread class way: As the child class overrides the run Thread class (), when calling start (), directly to the run () method (Java Virtual Machine automatically) subclass
that implements the Runnable way: Construction function passed a Runnable references, member variables passed to the Thread class, start () calls the run () method to determine when the internal member variable references Runnable is empty, if not empty, look at compile time is the Runnable run (), the runtime execution is a concrete implementation class run ()
the advantages and disadvantages:
inheritance Thread class way
benefit: You can use the Thread class directly, the code is simple
drawbacks: is also the object-oriented inheritance disadvantages: If the concrete class already has other parent, you can not multiple inheritance Thread class, you can not use this method. At this advantage oriented programming interface stand out.
Implement Runnable way
benefits: the inheritance of drawbacks: even if the thread class has their own definition of the other parent may also implement the Runnable interface. Java Interface is more than achievable, inheritance is single inheritance, more limited.
Drawbacks: the method can not be used directly in the Thread class, Runnable need to first transfer object to the implementation class Thread class and after obtaining the thread object, in order to obtain a Thread class method, the code is relatively complex

There is also a multi-threaded Callable if this method requires a return value of the call, the return value is submitted to the ExecutorService executed asynchronously, and may throw an exception

public static void main(String[] args) throws InterruptedException, ExecutionException {

// create a thread pool object
ExecutorService = Executors.newFixedThreadPool the pool (2);
// this is required here to instantiate a class object because here, MyCallable test is an internal class, similar to ordinary instance variables, such as a static class methods can not directly call the class instance variables. Here, the inside inner class is not static class, therefore, direct assignment (i.e., inner class instances), the program being given.
= new new Test Test Test ();
// submit task
Future <Integer> = F1 pool.submit (test.new MyCallable ());
Future <Integer> = F2 pool.submit (test.new MyCallable ());
Integer I f1.get = ();
Integer f2.get I1 = ();
System.out.println (I1);
// Close thread pool
pool.shutdown ();
}

class MyCallable implements Callable<Integer> {
public MyCallable() {
}

@Override
public Integer call() throws Exception {

int i = 0;
for (int x = 0; x < 100; x++) {
System.out.println(Thread.currentThread().getName() + ":" + x);

i++;
}
return i;
}
}

 



 

 

Guess you like

Origin www.cnblogs.com/duidui-li/p/11069900.html