?? 《java就業(yè)培訓(xùn)教程》_張孝祥_書內(nèi)源碼_05.txt
字號:
《Java就業(yè)培訓(xùn)教程》 作者:張孝祥 書中源碼
網(wǎng)址:www.itcast.cn
《Java就業(yè)培訓(xùn)教程》P177源碼
程序清單:ThreadDemo1.java
public class ThreadDemo1
{
public static void main(String args[])
{
new TestThread().run();
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName() +
" is running");
}
}
}
《Java就業(yè)培訓(xùn)教程》P180源碼
程序清單:ThreadDemo3.java
public class ThreadDemo3
{
public static void main(String args[])
{
//new TestThread ().start();
TestThread tt= new TestThread();//創(chuàng)建TestThread類的一個實例
Thread t= new Thread(tt);//創(chuàng)建一個Thread類的實例
t.start();//使線程進入Runnable狀態(tài)
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread implements Runnable //extends Thread
{
public void run()//線程的代碼段,當執(zhí)行start()時,線程從此出開始執(zhí)行
{
while(true)
{
System.out.println(Thread.currentThread().getName() +
" is running");
}
}
}
《Java就業(yè)培訓(xùn)教程》P181源碼
程序清單:ThreadDemo4.java
public class ThreadDemo4
{
public static void main(String [] args)
{
ThreadTest t=new ThreadTest();
t.start();
t.start();
t.start();
t.start();
}
}
class ThreadTest extends Thread
{
private int tickets=100;
public void run()
{
while(true)
{
if(tickets>0)
System.out.println(Thread.currentThread().getName() +
" is saling ticket " + tickets--);
}
}
}
《Java就業(yè)培訓(xùn)教程》P186源碼
程序清單:JoinThread.java
public class JoinThread
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
if(i==100)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread "+i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(true)
{
System.out.println(Thread.currentThread().getName()+" "+i++);
}
}
}
《Java就業(yè)培訓(xùn)教程》P195源碼
程序清單:ThreadDemo6.java
public class ThreadDemo6
{
public static void main(String [] args)
{
ThreadTest t=new ThreadTest();
new Thread(t).start();//這個線程調(diào)用同步代碼塊
t.str=new String("method");
new Thread(t).start();//這個線程調(diào)用同步函數(shù)
}
}
class ThreadTest implements Runnable
{
private int tickets=100;
String str = new String ("");
public void run()
{
if(str.equals("method"))
{
while(true)
{
sale();
}
}
else
{
while(true)
{
synchronized(str)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+
" is saling ticket " + tickets--);
}
}
}
}
}
public synchronized void sale()
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+
" is saling ticket " + tickets--);
}
}
}
《Java就業(yè)培訓(xùn)教程》P200源碼
程序清單:Deadlock.java
class A
{
synchronized void foo(B b)
{
String name=Thread.currentThread().getName();
System.out.println(name+ " entered A.foo ");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(name+ " trying to call B.last()");
b.last();
}
synchronized void last()
{
System.out.println("inside A.last");
}
}
class B
{
synchronized void bar(A a)
{
String name=Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(name + " trying to call A.last()");
a.last();
}
synchronized void last()
{
System.out.println("inside A.last");
}
}
class Deadlock implements Runnable
{
A a=new A();
B b=new B();
Deadlock()
{
Thread.currentThread().setName("MainThread");
new Thread(this).start();
a.foo(b); //get lock on a in this thread.
System.out.println("back in main thread");
}
public void run()
{
Thread.currentThread().setName("RacingThread");
b.bar(a); //get lock on a in other thread.
System.out.println("back in other thread");
}
public static void main(String[] args)
{
new Deadlock();
}
}
《Java就業(yè)培訓(xùn)教程》P204源碼
程序清單:ThreadCommunation.java
class Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
{
q.name="張孝祥";
q.sex="男";
}
else
{
q.name="陳瓊";
q.sex="女";
}
i=(i+1)%2;
}
}
}
class Q
{
String name="陳瓊";
String sex="女";
}
class Consumer implements Runnable
{
Q q=null;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
{
System.out.println(q.name + "---->" + q.sex);
}
}
}
public class ThreadCommunation
{
public static void main(String [] args)
{
Q q=new Q();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();
}
}
下載所得:
《Java就業(yè)培訓(xùn)教程》P177源碼
程序清單:ThreadDemo1.java
public class ThreadDemo1
{
public static void main(String args[])
{
new TestThread().run();
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName() +
" is running");
}
}
}
《Java就業(yè)培訓(xùn)教程》P180源碼
程序清單:ThreadDemo3.java
public class ThreadDemo3
{
public static void main(String args[])
{
//new TestThread ().start();
TestThread tt= new TestThread();//創(chuàng)建TestThread類的一個實例
Thread t= new Thread(tt);//創(chuàng)建一個Thread類的實例
t.start();//使線程進入Runnable狀態(tài)
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread implements Runnable //extends Thread
{
public void run()//線程的代碼段,當執(zhí)行start()時,線程從此出開始執(zhí)行
{
while(true)
{
System.out.println(Thread.currentThread().getName() +
" is running");
}
}
}
《Java就業(yè)培訓(xùn)教程》P181源碼
程序清單:ThreadDemo4.java
public class ThreadDemo4
{
public static void main(String [] args)
{
ThreadTest t=new ThreadTest();
t.start();
t.start();
t.start();
t.start();
}
}
class ThreadTest extends Thread
{
private int tickets=100;
public void run()
{
while(true)
{
if(tickets>0)
System.out.println(Thread.currentThread().getName() +
" is saling ticket " + tickets--);
}
}
}
《Java就業(yè)培訓(xùn)教程》P186源碼
程序清單:JoinThread.java
public class JoinThread
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
if(i==100)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread "+i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(true)
{
System.out.println(Thread.currentThread().getName()+" "+i++);
}
}
}
《Java就業(yè)培訓(xùn)教程》P195源碼
程序清單:ThreadDemo6.java
public class ThreadDemo6
{
public static void main(String [] args)
{
ThreadTest t=new ThreadTest();
new Thread(t).start();//這個線程調(diào)用同步代碼塊
t.str=new String("method");
new Thread(t).start();//這個線程調(diào)用同步函數(shù)
}
}
class ThreadTest implements Runnable
{
private int tickets=100;
String str = new String ("");
public void run()
{
if(str.equals("method"))
{
while(true)
{
sale();
}
}
else
{
while(true)
{
synchronized(str)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+
" is saling ticket " + tickets--);
}
}
}
}
}
public synchronized void sale()
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+
" is saling ticket " + tickets--);
}
}
}
《Java就業(yè)培訓(xùn)教程》P200源碼
程序清單:Deadlock.java
class A
{
synchronized void foo(B b)
{
String name=Thread.currentThread().getName();
System.out.println(name+ " entered A.foo ");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(name+ " trying to call B.last()");
b.last();
}
synchronized void last()
{
System.out.println("inside A.last");
}
}
class B
{
synchronized void bar(A a)
{
String name=Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(name + " trying to call A.last()");
a.last();
}
synchronized void last()
{
System.out.println("inside A.last");
}
}
class Deadlock implements Runnable
{
A a=new A();
B b=new B();
Deadlock()
{
Thread.currentThread().setName("MainThread");
new Thread(this).start();
a.foo(b); //get lock on a in this thread.
System.out.println("back in main thread");
}
public void run()
{
Thread.currentThread().setName("RacingThread");
b.bar(a); //get lock on a in other thread.
System.out.println("back in other thread");
}
public static void main(String[] args)
{
new Deadlock();
}
}
《Java就業(yè)培訓(xùn)教程》P204源碼
程序清單:ThreadCommunation.java
class Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
{
q.name="張孝祥";
q.sex="男";
}
else
{
q.name="陳瓊";
q.sex="女";
}
i=(i+1)%2;
}
}
}
class Q
{
String name="陳瓊";
String sex="女";
}
class Consumer implements Runnable
{
Q q=null;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
{
System.out.println(q.name + "---->" + q.sex);
}
}
}
public class ThreadCommunation
{
public static void main(String [] args)
{
Q q=new Q();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -