Thread
1. ready
// 模拟一个比较耗时的操作
public static void sayHello() {
String hello = "Hello Worldn";
char[] charArray = hello.toCharArray();
for (char ch : charArray) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(ch);
}
}
2. new
- 继承 Thread 重写 run 方法
Thread task = new Thread(){ @Override public void run() { sayHello(); } };
- 实现 Runnable 接口,将实现类对象作为参数传递给 Thread 的构造方法
Runnable runnable = new Runnable() { @Override public void run() { sayHello(); } }; Thread thread = new Thread(runnable);
3. run
- 运行
public static void main(String[] args) { task.run(); System.out.println("End"); }
- 输出
Hello World
End - Thread 的 run
public class Thread implements Runnable { private Runnable target; ... public Thread(){}; public Thread(Runnable target){ ... this.target = target; ... } @Override public void run(){ if(target != null){ target.run(); } } }
- run 只是个普通的方法
4. start
- 运行
public static void main(String[] args) throws InterruptedException { task.start(); System.out.println("End"); task.join(); }
- 输出
End
Hello World
5. synchronized
public class SynchronizedTest {
private static Integer count = 0;
private void doTask(Runnable task) {
try {
List<Thread> taskList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
taskList.add(new Thread(task));
}
taskList.forEach(Thread::start);
for (Thread thread : taskList) {
thread.join();
}
System.out.println(count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* count < 5000000
*/
@Test
public void test01() {
doTask(() -> {
for (int j = 0; j < 1000000; j++) {
count++;
}
});
}
/**
* count < 5000000
*/
@Test
public void test02() {
doTask(() -> {
for (int j = 0; j < 1000000; j++) {
synchronized (count) {
count++;
}
}
});
}
/**
* count = 5000000
*/
@Test
public void test03() {
Object lock = new Object();
doTask(() -> {
for (int i = 0; i < 1000000; i++) {
synchronized (lock) {
count++;
}
}
});
}
/**
* count = 5000000
*/
@Test
public void test04() {
ReentrantLock lock = new ReentrantLock();
doTask(() -> {
for (int i = 0; i < 1000000; i++) {
lock.lock();
count++;
lock.unlock();
}
});
}
}
6. wait and notify
- code
public class WaitAndNotify { public static void main(String[] args) throws InterruptedException { Object semaphore = new Object(); Thread thread1 = new Thread(() -> { synchronized (semaphore) { System.out.println("thread1 have semaphore"); System.out.println("thread1 start wait"); try { semaphore.wait(); System.out.println("thread1 end wait"); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread thread2 = new Thread(()->{ synchronized (semaphore) { System.out.println("thread2 have semaphore"); System.out.println("thread2 start notify"); semaphore.notify(); System.out.println("thread2 end notify"); } }); thread1.start(); Thread.sleep(100); thread2.start(); thread1.join(); thread2.join(); } }
- out
thread1 have semaphore
thread1 start wait
thread2 have semaphore
thread2 start notify
thread2 end notify
thread1 end wait
7. Condition
- code
public class ConditionTest { public static void main(String[] args) throws InterruptedException { ReentrantLock lock = new ReentrantLock(); Condition condition = lock.newCondition(); Thread thread1 = new Thread(() -> { lock.lock(); System.out.println("thread1 have semaphore"); System.out.println("thread1 start wait"); try { condition.await(); System.out.println("thread1 end wait"); } catch (InterruptedException e) { e.printStackTrace(); } lock.unlock(); }); Thread thread2 = new Thread(() -> { lock.lock(); System.out.println("thread2 have semaphore"); System.out.println("thread2 start notify"); condition.signal(); System.out.println("thread2 end notify"); lock.unlock(); }); thread1.start(); Thread.sleep(100); thread2.start(); thread1.join(); thread2.join(); } }
- out
thread1 have semaphore
thread1 start wait
thread2 have semaphore
thread2 start notify
thread2 end notify
thread1 end wait
近期评论