一、创建线程的方式
1.继承Thread类与线程的常用方法
功能类继承Thread 实例化功能类对象 功能类对象.start()
package thread ;
public class ThreadTest {
public static void main ( String [ ] args) throws InterruptedException {
TestThread1 thread1 = new TestThread1 ( ) ;
thread1. setPriority ( Thread . MAX_PRIORITY) ;
thread1. start ( ) ;
for ( int i = 1 ; i <= 100 ; i++ ) {
if ( i % 2 == 0 ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "--" + i) ;
}
if ( i == 49 ) {
thread1. join ( ) ;
}
if ( i == 80 ) {
Thread . currentThread ( ) . stop ( ) ;
}
System . out. println ( thread1. isAlive ( ) ) ;
}
}
}
class TestThread1 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 100 ; i++ ) {
if ( i % 2 == 0 ) {
Thread . currentThread ( ) . setName ( this . getClass ( ) . getName ( ) ) ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "--" + i) ;
}
if ( i % 20 == 0 ) {
yield ( ) ;
}
if ( i % 30 == 0 ) {
try {
sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
2.实现Runnable接口
功能类实现Runnable接口,并重写run方法 功能类对象作为Thread构造器的参数创建Thread对象 Thread对象.start()
package thread ;
public class RunnableTest {
public static void main ( String [ ] args) {
TestThread tt = new TestThread ( ) ;
Thread thread = new Thread ( tt) ;
Thread thread1 = new Thread ( tt) ;
Thread thread2 = new Thread ( tt) ;
thread. start ( ) ;
thread1. start ( ) ;
thread2. start ( ) ;
}
}
class TestThread implements Runnable {
@Override
public void run ( ) {
for ( int i = 1 ; i <= 100 ; i++ ) {
if ( i % 2 == 0 ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "--" + i) ;
}
}
}
}
3.实现Callable接口
package thread. ticket ;
import java. util. concurrent. Callable ;
import java. util. concurrent. ExecutionException ;
import java. util. concurrent. FutureTask ;
public class Window implements Callable < Integer > {
private int ticketCount = 100 ;
@Override
public Integer call ( ) {
while ( ticketCount > 0 ) {
synchronized ( this ) {
int i = 101 - ticketCount;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "出售第" + i + "张票" ) ;
ticketCount -= 1 ;
}
}
return ticketCount;
}
public static void main ( String [ ] args) throws ExecutionException , InterruptedException {
Window window = new Window ( ) ;
FutureTask futureTask = new FutureTask ( window) ;
FutureTask futureTask1 = new FutureTask ( window) ;
FutureTask futureTask2 = new FutureTask ( window) ;
Thread thread = new Thread ( futureTask) ;
Thread thread1 = new Thread ( futureTask) ;
Thread thread2 = new Thread ( futureTask) ;
thread1. setName ( "aaaa" ) ;
thread. setName ( "bbbb" ) ;
thread2. setName ( "cccc" ) ;
thread. start ( ) ;
thread1. start ( ) ;
thread2. start ( ) ;
Object o = futureTask. get ( ) ;
System . out. println ( o) ;
}
}
二、解决线程安全问题
1.同步代码块
package thread. ticket ;
public class Window implements Runnable {
public int ticketCount = 100 ;
@Override
public void run ( ) {
synchronized ( this ) {
while ( ticketCount> 0 ) {
int i = 101 - ticketCount;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "出售第" + i+ "张票" ) ;
ticketCount -= 1 ;
}
}
}
public static void main ( String [ ] args) {
Window w1 = new Window ( ) ;
Thread thread = new Thread ( w1) ;
Thread thread1 = new Thread ( w1) ;
Thread thread2 = new Thread ( w1) ;
thread1. start ( ) ;
thread. start ( ) ;
thread2. start ( ) ;
}
}
2.同步方法
package thread. ticket ;
public class Window implements Runnable {
public int ticketCount = 100 ;
@Override
public void run ( ) {
sale ( ) ;
}
public synchronized void sale ( ) {
while ( ticketCount> 0 ) {
int i = 101 - ticketCount;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "出售第" + i+ "张票" ) ;
ticketCount -= 1 ;
}
}
public static void main ( String [ ] args) {
Window w1 = new Window ( ) ;
Thread thread = new Thread ( w1) ;
Thread thread1 = new Thread ( w1) ;
Thread thread2 = new Thread ( w1) ;
thread1. start ( ) ;
thread. start ( ) ;
thread2. start ( ) ;
}
}
3.与synchronized 相关的三个方法
package thread. ticket ;
import java. util. concurrent. locks. ReentrantLock ;
public class Window implements Runnable {
private int ticketCount = 100 ;
@Override
public void run ( ) {
while ( ticketCount > 0 ) {
synchronized ( this ) {
notifyAll ( ) ;
int i = 101 - ticketCount;
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( Thread . currentThread ( ) . getName ( ) + "出售第" + i + "张票" ) ;
ticketCount -= 1 ;
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
public static void main ( String [ ] args) {
Window window = new Window ( ) ;
Thread thread1 = new Thread ( window) ;
Thread thread2 = new Thread ( window) ;
Thread thread3 = new Thread ( window) ;
thread1. start ( ) ;
thread2. start ( ) ;
thread3. start ( ) ;
}
}
4.Lock锁方式(JDK5)
package thread. ticket ;
import java. util. concurrent. locks. ReentrantLock ;
public class Window implements Runnable {
private int ticketCount = 100 ;
private ReentrantLock lock = new ReentrantLock ( ) ;
@Override
public void run ( ) {
lock. lock ( ) ;
try {
while ( ticketCount> 0 ) {
int i = 101 - ticketCount;
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( Thread . currentThread ( ) . getName ( ) + "出售第" + i+ "张票" ) ;
ticketCount -= 1 ;
}
} finally {
lock. unlock ( ) ;
}
}
public static void main ( String [ ] args) {
Window window = new Window ( ) ;
Thread thread1 = new Thread ( window) ;
Thread thread2 = new Thread ( window) ;
Thread thread3 = new Thread ( window) ;
thread1. start ( ) ;
thread2. start ( ) ;
thread3. start ( ) ;
}
}
(附)死锁示例
package thread. ticket ;
public class Window implements Runnable {
public static void main ( String [ ] args) {
StringBuffer aa = new StringBuffer ( "aa" ) ;
StringBuffer bb = new StringBuffer ( "bb" ) ;
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
synchronized ( aa) {
aa. append ( "cc" ) ;
bb. append ( "dd" ) ;
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
synchronized ( bb) {
aa. append ( "11" ) ;
bb. append ( "22" ) ;
System . out. println ( aa) ;
System . out. println ( bb) ;
}
}
}
} ) . start ( ) ;
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
synchronized ( bb) {
aa. append ( "11" ) ;
bb. append ( "22" ) ;
synchronized ( aa) {
aa. append ( "cc" ) ;
bb. append ( "dd" ) ;
System . out. println ( aa) ;
System . out. println ( bb) ;
}
}
}
} ) . start ( ) ;
}
}