多线程
This commit is contained in:
46
code/src/main/java/com/raorao/java/thread/MyThread.java
Normal file
46
code/src/main/java/com/raorao/java/thread/MyThread.java
Normal file
@ -0,0 +1,46 @@
|
||||
package com.raorao.java.thread;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
/**
|
||||
* 线程类.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-01-15:40
|
||||
*/
|
||||
public class MyThread extends Thread {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Thread t1 = new MyThread();
|
||||
t1.start();
|
||||
Thread t2 = new Thread(new MyThread2());
|
||||
t2.start();
|
||||
Callable call = new MyCallable();
|
||||
FutureTask<String> task = new FutureTask<String>(call);
|
||||
Thread t3 = new Thread(task);
|
||||
t3.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("线程运行中---------extends");
|
||||
}
|
||||
|
||||
static class MyThread2 implements Runnable{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("线程运行中------runnable");
|
||||
}
|
||||
}
|
||||
|
||||
static class MyCallable implements Callable{
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
System.out.println("线程运行中------callable");
|
||||
return "call over!";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.raorao.java.thread.excutor;
|
||||
|
||||
import afu.org.checkerframework.checker.igj.qual.I;
|
||||
import com.raorao.java.thread.excutor.MyExecutor.MyRunnable;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import javax.sound.midi.Soundbank;
|
||||
import org.omg.PortableServer.THREAD_POLICY_ID;
|
||||
|
||||
/**
|
||||
* futureTask测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-02-11:16
|
||||
*/
|
||||
public class FutureTaskTest {
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
Integer result = 0;
|
||||
FutureTask<Integer> task1 = new FutureTask<>(new MyRunnable("future thread"), result);
|
||||
// task1.run();
|
||||
// System.out.println(task1.get());
|
||||
|
||||
// FutureTask<Integer> task2 = new FutureTask<>(new MyCallable(5));
|
||||
// task2.run();
|
||||
// System.out.println(task2.get());
|
||||
ExecutorService service = Executors.newFixedThreadPool(1);
|
||||
Future<Integer> res = service.submit(new MyCallable(5));
|
||||
System.out.println(res.get());
|
||||
System.out.println("ok");
|
||||
Thread.sleep(2000);
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
static class MyCallable implements Callable<Integer>{
|
||||
|
||||
private Integer res = 0;
|
||||
public MyCallable(Integer a) {
|
||||
res = a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
//Thread.sleep(2000);
|
||||
wait(2000);
|
||||
return res * res;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.raorao.java.thread.excutor;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 可重入锁测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-02-11:14
|
||||
*/
|
||||
public class LockTest {
|
||||
|
||||
private Lock lock = new ReentrantLock();
|
||||
|
||||
public static void main(String[] args) {
|
||||
LockTest test = new LockTest();
|
||||
ExecutorService service = Executors.newFixedThreadPool(3);
|
||||
for(int i = 0 ; i < 3 ; i ++){
|
||||
service.submit(() -> test.func());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void func() {
|
||||
lock.lock();
|
||||
try {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
} finally {
|
||||
lock.unlock(); // 确保释放锁,从而避免发生死锁。
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.raorao.java.thread.excutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import javax.sound.midi.Soundbank;
|
||||
import javax.sound.midi.Track;
|
||||
|
||||
/**
|
||||
* Executor框架.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-01-16:32
|
||||
*/
|
||||
public class MyExecutor {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExecutorService service = Executors.newFixedThreadPool(3);
|
||||
for(int i = 0; i < 3; i++){
|
||||
service.submit(new MyRunnable("thread-" + i));
|
||||
}
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
static class MyRunnable implements Runnable{
|
||||
|
||||
private String name;
|
||||
|
||||
public MyRunnable(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(name + " start ...");
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(name + " end ...");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user