⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testthread3.java

📁 自己做的一个QQ聊天系统
💻 JAVA
字号:
package test.thread;public class TestThread3 {	public static void main(String[] args) throws InterruptedException {		// TODO Auto-generated method stub        TestSynchronized t = new TestSynchronized();        t.add.start();        t.sub.start();        Thread.sleep(6000);        System.out.println("i =" + t.i);	}}class TestSynchronized{	    int i = 200;	    Thread add;	    Thread sub;	    public TestSynchronized(){	    	 add = new Thread(){	    		 public void run(){	    			 for(int j=0;j<100;j++){	    				 try{	    					 Thread.sleep(50);	    				 } catch (InterruptedException e) {	    					 e.printStackTrace();	    				 }	    				 add();	    			 }	    		 }	    		 	    	 };	    	 sub = new Thread(){	    		 public void run(){	    			 for(int j=0;j<100;j++){	    				 try{	    					 Thread.sleep(50);	    				 } catch (InterruptedException e) {	    					 e.printStackTrace();	    				 }	    				 sub();	    			 }	    		 }	    	 };	    	 	    }	    public synchronized void add(){			i = i+1;		}		public synchronized void sub(){			i = i-1;		} }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -