《Java就业培训教程》_张孝祥_书内源码_05

《Java就业培训教程》 作者:张孝祥 书中源码
《Java就业培训教程》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就业培训教程》P180源码
程序清单:ThreadDemo3.java
public class ThreadDemo3
{
 public static void main(String args[])
 {
  //new TestThread ().start();
  TestThread tt= new TestThread();//创建TestThread类的一个实例
  Thread t= new Thread(tt);//创建一个Thread类的实例
  t.start();//使线程进入Runnable状态
  while(true)
  {
   System.out.println("main thread is running");
  }
 }
}
class TestThread implements Runnable //extends Thread
{
 public void run()//线程的代码段,当执行start()时,线程从此出开始执行
 {
  while(true)
  {
   System.out.println(Thread.currentThread().getName() +
   " is running");
  }
 }
}

《Java就业培训教程》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就业培训教程》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就业培训教程》P195源码
程序清单:ThreadDemo6.java
public class ThreadDemo6
{
 public static void main(String [] args)
 {
  ThreadTest t=new ThreadTest();
  new Thread(t).start();//这个线程调用同步代码块
  t.str=new String("method");
  new Thread(t).start();//这个线程调用同步函数
 }
}
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就业培训教程》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就业培训教程》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,thread,string,exception,class)