例子:同时下载文件和播放音乐
class Player extends Thread{
public void run()
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("音乐播放"+i*10+"%");
}
}
}
class DownLoader extends Thread{//②
public void run ()//①
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("文件下载"+i*10+"%");
}
}
}
class Test{
public static void main (String[] args) {
Player p = new Player();
DownLoader d = new DownLoader();
p.start();//③
d.start();
}
}
class Player extends Thread{
public void run() {
this.play();
}
public void play()
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("音乐播放"+i*10+"%");
}
}
}
class DownLoader extends Thread{//②
public void run() {
this.play();
}
public void play ()//①
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("文件下载"+i*10+"%");
}
}
}
class Test{
public static void main (String[] args) {
Player p = new Player();
DownLoader d = new DownLoader();
p.start();//③
d.start();
}
}
class Downloader implements Runnable{//①
public void run(){ //②
for(int i=1;i<=10;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("下载进度:" + i*10 + "%");
}
}
}
class Player implements Runnable{
public void run(){
for(int i=1;i<=10;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("播放进度:" + i*10 + "%");
}
}
}
class Test{
public static void main (String[] args) {
Downloader d = new Downloader();
Player p = new Player();
Thread t1 = new Thread(d); //③
Thread t2 = new Thread(p);
t1.start();
t2.start();
}
}
例子:播放音乐,3秒之后,暂停5秒,再次运行
class Downloader extends Thread{//①
public void run(){ //②
for(int i=1;i<=10;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("下载进度:" + i*10 + "%");
}
}
}
class Test{
public static void main (String[] args) throws Exception{
Downloader d = new Downloader();
d.start();
Thread.sleep(3000);
d.suspend();
Thread.sleep(4000);
d.resume();
Thread.sleep(2000);
d.stop();
}
}
----以上方法是不安全的,要避免使用!因为以上方法有deadlock-prone(可能造成死锁)
package customer;
class Downloader extends Thread{//①
boolean RUN = true;
static int percent = 1;
public void run(){ //②
for(int i=percent;i<=10 && RUN;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("下载进度:" + i*10 + "%");
percent = i+1;
}
}
}
class Test{
public static void main (String[] args) throws Exception{
Downloader d = new Downloader();
d.start();
Thread.sleep(3000);
d.RUN = false;
Thread.sleep(4000);
d = new Downloader();
d.start();
Thread.sleep(2000);
d.RUN = false;
}
}
多个线程访问同一资源。
两个线程进行文件下载。
class Downloader extends Thread{//①
boolean RUN = true;
int i = 0;
public void run(){ //②
while(RUN){
try{ Thread.sleep(1000);} catch(Exception e){}//这个很重要,不能在sy里面
synchronized(this){//这段代码运行时CPU不会被抢占
i++;
System.out.println("文件下载:" + i*10 + "%");
if(i>=9) break;
}
}
}
}
class Test{
public static void main (String[] args) throws Exception{
Downloader d = new Downloader();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
t2.start();
}
}
synchronized:同步代码,保持原子性(代码运行中,别的线程不能抢占)