创建线程的三种方式
public class Test01 {
public static void main ( String[ ] args) {
ThreadImpl01 thread = new ThreadImpl01 ( ) ;
thread. start ( ) ;
}
}
class ThreadImpl01 extends Thread {
@Override
public void run ( ) {
System. out. println ( "线程程序开始执行..." ) ;
}
}
public class Test02 {
public static void main ( String[ ] args) {
ThreadImpl02 runnalbe = new ThreadImpl02 ( ) ;
Thread thread = new Thread ( runnalbe) ;
thread. start ( ) ;
}
}
class ThreadImpl02 implements Runnable {
@Override
public void run ( ) {
System. out. println ( "线程程序开始执行" ) ;
}
}
public class Test03 {
public static void main ( String[ ] args) throws ExecutionException, InterruptedException {
FutureTask< Object> task = new FutureTask < > ( new Callable < Object> ( ) {
@Override
public Object call ( ) throws Exception {
System. out. println ( "线程程序开始执行" ) ;
return 100 ;
}
} ) ;
Thread thread = new Thread ( task) ;
thread. start ( ) ;
Object result = task. get ( ) ;
System. out. println ( result) ;
}
}
synchronized出现在三个位置的效果
public class SynchronizedTest {
public int test01 ( ) {
synchronized ( this ) {
int a = 1 ;
int b = 2 ;
return a + b;
}
}
public synchronized int test02 ( ) {
int a = 1 ;
int b = 2 ;
return a + b;
}
public synchronized static int test03 ( ) {
int a = 1 ;
int b = 2 ;
return a + b;
}
}
死锁代码实现:
public class DeadLock {
public static void main ( String[ ] args) {
Object o1 = new Object ( ) ;
Object o2 = new Object ( ) ;
MyThread1 t1 = new MyThread1 ( o1, o2) ;
MyThread2 t2 = new MyThread2 ( o1, o2) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
class MyThread1 extends Thread {
Object o1;
Object o2;
public MyThread1 ( Object o1, Object o2) {
this . o1 = o1;
this . o2 = o2;
}
@Override
public void run ( ) {
synchronized ( o1) {
System. out. println ( "t1线程获取到o1的对象锁,还需要获取到o2的对象锁" ) ;
synchronized ( o2) {
System. out. println ( "t1获取到o2的对象锁" ) ;
}
}
}
}
class MyThread2 extends Thread {
Object o1;
Object o2;
public MyThread2 ( Object o1, Object o2) {
this . o1 = o1;
this . o2 = o2;
}
@Override
public void run ( ) {
synchronized ( o2) {
System. out. println ( "t2线程获取到o2的对象锁,还需要获取到o1的对象锁" ) ;
synchronized ( o1) {
System. out. println ( "t2获取到o1的对象锁" ) ;
}
}
}
}
Java定时器的实现
public class TimerTest {
public static void main ( String[ ] args) throws ParseException {
Timer timer = new Timer ( ) ;
LogTimerTask task = new LogTimerTask ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date firstTime = sdf. parse ( "2020-8-29 12:45:00" ) ;
timer. schedule ( task, firstTime, 1000 * 10 ) ;
}
}
class LogTimerTask extends TimerTask {
@Override
public void run ( ) {
System. out. println ( "定时器正在执行定时任务..." ) ;
}
}
生产者和消费者模式
public class Test01 {
public static void main ( String[ ] args) {
List list = new ArrayList ( ) ;
Thread t1 = new Thread ( new Producer ( list) ) ;
t1. setName ( "生产者线程" ) ;
Thread t2 = new Thread ( new Consumer ( list) ) ;
t2. setName ( "消费者线程" ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
class Producer implements Runnable {
private List list;
public Producer ( List list) {
this . list = list;
}
@Override
public void run ( ) {
while ( true ) {
synchronized ( list) {
if ( list. size ( ) > 0 ) {
try {
list. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
String product = "产品" ;
list. add ( product) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "正在生产-->" + product) ;
list. notify ( ) ;
}
}
}
}
class Consumer implements Runnable {
private List list;
public Consumer ( List list) {
this . list = list;
}
@Override
public void run ( ) {
while ( true ) {
synchronized ( list) {
if ( list. size ( ) == 0 ) {
try {
list. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
list. removeAll ( list) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "-->正在消费.." ) ;
list. notify ( ) ;
}
}
}
}