http://developer.51cto.com/art/200512/15883.htm
线程总复习创建 线程方式 (1)继承Thread(2)实现Runnable 注意: 在类中要重写run()方法启动线程 第一种直可以实例化线程类用start()启动 第二种也要实例线程类。之后在构建一个Thread实例,将线程实例名作为Thread的参数 参考实例:http://hi.bccn.net/space-447825-do-blog-id-17017.html Thread的相关方法 isDaemon()测试该线程是否为守护线程(后台线程) setDaemon...
public class SyncTest{ public static void main(String args[]){ SyncStack stack = new SyncStack(); Runnable p=new Producer(stack); Runnable c = new Consumer(stack); Thread t1 = new Thread(p); Thread t2 = new Thread(c); t1.start(); t2.start(); ...
public class TestDeadLock{ public static void main(String args[]){ StringBuffer sb = new StringBuffer("ABCD"); MyThread t = new MyThread(sb); t.start(); synchronized(sb){ try{ t.join(); }catch(InterruptedException e){ ...
class Stack{ int idx=0; char[ ] data = new char[6]; public void push(char c){ data[idx] = c; System.out.print("压入字符" + c + "->"); //p1 idx++; System.out.println("->压入" + c + "操作完成!"); } public char pop(){ idx--; return data[idx];...
import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JButton;public class TestSuspend{ public static void main(String[] args){ JFrame jf = new JFrame("T...
import java.util.Date;public class TestYield{ public static void main(String[] args){ Thread t1 = new MyThread(false); Thread t2 = new MyThread(true); Thread t3 = new MyThread(false); t1.start(); t2.start(); t3.start(); } }class MyThread extend...
public class TestJoin { public static void main(String args[]){ MyRunner r = new MyRunner(); Thread t = new Thread(r); t.start(); try{ t.join(); }catch(InterruptedException e){ e.printStackTrace(); } for(int i=0;i<50;...
public class TestPriority { public static void main(String args[]){ System.out.println("线程名\t优先级"); Thread current = Thread.currentThread(); System.out.print(current.getName() + "\t"); System.out.println(current.getPriority()); Thread t1 = new R...
import java.awt.*;import java.awt.event.*;public class TestGUIThread { public static void main(String args[]) throws Exception{ Frame f = new Frame(); Button b = new Button("Press Me!"); MyMonitor mm = new MyMonitor(); b.addActionListener(mm); f...
public class TestDaemonThread { public static void main(String args[]){ Thread t1 = new MyRunner(10); t1.setName("用户线程t1"); t1.start(); Thread t2 = new MyRunner(100); t2.setDaemon(true); t2.setName("后台线程t2"); t2.start(); ...
public class Test1 {//创建和启动线程 public static void main(String[] args) { //启动线程1 Thread1 t1=new Thread1(); t1.start(); //启动线程2 Thread2 t2=new Thread2(); Thread t2i=new Thread(t2); t2i.start(); }}//创建一个线程的第一种方式class Thread1 extends Thread{ public void run(){ ...