本文目錄一覽:
java跟C#是可以通用的嗎
在某種程度上來說是通用的,因為他們都是可以通用的接口,就是說,java中可以調用才C#的代碼,C#通過接口,或者類庫的方式也可以調用java.
設置上大部分一樣,但是語法,變量設置上有很多的不同,不知道你問的通用是指什麼,如果是調用的話,可以通過通用接口,如果是直接粘貼進去的話,是肯定不行的。
java通用版是什麼意思
java 的分辯率一定的,只不過通用的可能有有多種解決方案,如即支持小屏又支持大屏。通用版是針對低端手機java 兼容性差而出的,所以功能與界面一般比專用版差,所以你要是能在不載界面找到自己的手機型號,還是下專用版,如果找不到就只好下通用的了。
Java實現通用線程池
線程池通俗的描述就是預先創建若干空閑線程 等到需要用多線程去處理事務的時候去喚醒某些空閑線程執行處理任務 這樣就省去了頻繁創建線程的時間 因為頻 繁創建線程是要耗費大量的CPU資源的 如果一個應用程序需要頻繁地處理大量並發事務 不斷的創建銷毀線程往往會大大地降低系統的效率 這時候線程池就派 上用場了
本文旨在使用Java語言編寫一個通用的線程池 當需要使用線程池處理事務時 只需按照指定規範封裝好事務處理對象 然後用已有的線程池對象去自動選擇空 閑線程自動調用事務處理對象即可 並實現線程池的動態修改(修改當前線程數 最大線程數等) 下面是實現代碼
//ThreadTask java
package polarman threadpool;
/** *//**
*線程任務
* @author ryang
*
*/
public interface ThreadTask {
public void run();
}
//PooledThread java
package polarman threadpool;
import java util Collection; import java util Vector;
/** *//**
*接受線程池管理的線程
* @author ryang
*
*/
public class PooledThread extends Thread {
protected Vector tasks = new Vector();
protected boolean running = false;
protected boolean stopped = false;
protected boolean paused = false;
protected boolean killed = false;
private ThreadPool pool;
public PooledThread(ThreadPool pool) { this pool = pool;
}
public void putTask(ThreadTask task) { tasks add(task);
}
public void putTasks(ThreadTask[] tasks) { for(int i= ; itasks length; i++) this tasks add(tasks[i]);
}
public void putTasks(Collection tasks) { this tasks addAll(tasks);
}
protected ThreadTask popTask() { if(tasks size() ) return (ThreadTask)tasks remove( );
else
return null;
}
public boolean isRunning() {
return running;
}
public void stopTasks() {
stopped = true;
}
public void stopTasksSync() {
stopTasks();
while(isRunning()) { try {
sleep( );
} catch (InterruptedException e) {
}
}
}
public void pauseTasks() {
paused = true;
}
public void pauseTasksSync() {
pauseTasks();
while(isRunning()) { try {
sleep( );
} catch (InterruptedException e) {
}
}
}
public void kill() { if(!running)
interrupt();
else
killed = true;
}
public void killSync() {
kill();
while(isAlive()) { try {
sleep( );
} catch (InterruptedException e) {
}
}
}
public synchronized void startTasks() {
running = true;
this notify();
}
public synchronized void run() { try { while(true) { if(!running || tasks size() == ) { pool notifyForIdleThread(); //System out println(Thread currentThread() getId() + : 空閑 ); this wait(); }else {
ThreadTask task;
while((task = popTask()) != null) { task run(); if(stopped) {
stopped = false;
if(tasks size() ) { tasks clear(); System out println(Thread currentThread() getId() + : Tasks are stopped );
break;
}
}
if(paused) {
paused = false;
if(tasks size() ) { System out println(Thread currentThread() getId() + : Tasks are paused );
break;
}
}
}
running = false;
}
if(killed) {
killed = false;
break;
}
}
}catch(InterruptedException e) {
return;
}
//System out println(Thread currentThread() getId() + : Killed );
}
}
//ThreadPool java
package polarman threadpool;
import java util Collection; import java util Iterator; import java util Vector;
/** *//**
*線程池
* @author ryang
*
*/
public class ThreadPool {
protected int maxPoolSize;
protected int initPoolSize;
protected Vector threads = new Vector();
protected boolean initialized = false;
protected boolean hasIdleThread = false;
public ThreadPool(int maxPoolSize int initPoolSize) { this maxPoolSize = maxPoolSize; this initPoolSize = initPoolSize;
}
public void init() {
initialized = true;
for(int i= ; iinitPoolSize; i++) {
PooledThread thread = new PooledThread(this);
thread start(); threads add(thread);
}
//System out println( 線程池初始化結束 線程數= + threads size() + 最大線程數= + maxPoolSize);
}
public void setMaxPoolSize(int maxPoolSize) { //System out println( 重設最大線程數 最大線程數= + maxPoolSize); this maxPoolSize = maxPoolSize;
if(maxPoolSize getPoolSize())
setPoolSize(maxPoolSize);
}
/** *//**
*重設當前線程數
* 若需殺掉某線程 線程不會立刻殺掉 而會等到線程中的事務處理完成* 但此方法會立刻從線程池中移除該線程 不會等待事務處理結束
* @param size
*/
public void setPoolSize(int size) { if(!initialized) {
initPoolSize = size;
return;
}else if(size getPoolSize()) { for(int i=getPoolSize(); isize imaxPoolSize; i++) {
PooledThread thread = new PooledThread(this);
thread start(); threads add(thread);
}
}else if(size getPoolSize()) { while(getPoolSize() size) { PooledThread th = (PooledThread)threads remove( ); th kill();
}
}
//System out println( 重設線程數 線程數= + threads size());
}
public int getPoolSize() { return threads size();
}
protected void notifyForIdleThread() {
hasIdleThread = true;
}
protected boolean waitForIdleThread() {
hasIdleThread = false;
while(!hasIdleThread getPoolSize() = maxPoolSize) { try { Thread sleep( ); } catch (InterruptedException e) {
return false;
}
}
return true;
}
public synchronized PooledThread getIdleThread() { while(true) { for(Iterator itr=erator(); itr hasNext();) { PooledThread th = (PooledThread)itr next(); if(!th isRunning())
return th;
}
if(getPoolSize() maxPoolSize) {
PooledThread thread = new PooledThread(this);
thread start(); threads add(thread);
return thread;
}
//System out println( 線程池已滿 等待 );
if(waitForIdleThread() == false)
return null;
}
}
public void processTask(ThreadTask task) {
PooledThread th = getIdleThread();
if(th != null) { th putTask(task); th startTasks();
}
}
public void processTasksInSingleThread(ThreadTask[] tasks) {
PooledThread th = getIdleThread();
if(th != null) { th putTasks(tasks); th startTasks();
}
}
public void processTasksInSingleThread(Collection tasks) {
PooledThread th = getIdleThread();
if(th != null) { th putTasks(tasks); th startTasks();
}
}
}
下面是線程池的測試程序
//ThreadPoolTest java
import java io BufferedReader; import java io IOException; import java io InputStreamReader;
import polarman threadpool ThreadPool; import polarman threadpool ThreadTask;
public class ThreadPoolTest {
public static void main(String[] args) { System out println( quit 退出 ); System out println( task A 啟動任務A 時長為 秒 ); System out println( size 設置當前線程池大小為 ); System out println( max 設置線程池最大線程數為 ); System out println();
final ThreadPool pool = new ThreadPool( ); pool init();
Thread cmdThread = new Thread() { public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System in));
while(true) { try { String line = reader readLine(); String words[] = line split( ); if(words[ ] equalsIgnoreCase( quit )) { System exit( ); }else if(words[ ] equalsIgnoreCase( size ) words length = ) { try { int size = Integer parseInt(words[ ]); pool setPoolSize(size); }catch(Exception e) {
}
}else if(words[ ] equalsIgnoreCase( max ) words length = ) { try { int max = Integer parseInt(words[ ]); pool setMaxPoolSize(max); }catch(Exception e) {
}
}else if(words[ ] equalsIgnoreCase( task ) words length = ) { try { int timelen = Integer parseInt(words[ ]); SimpleTask task = new SimpleTask(words[ ] timelen * ); pool processTask(task); }catch(Exception e) {
}
}
} catch (IOException e) { e printStackTrace();
}
}
}
};
cmdThread start();
/**//*
for(int i= ; i ; i++){
SimpleTask task = new SimpleTask( Task + i (i+ )* ); pool processTask(task);
}*/
}
}
class SimpleTask implements ThreadTask {
private String taskName;
private int timeLen;
public SimpleTask(String taskName int timeLen) { this taskName = taskName; this timeLen = timeLen;
}
public void run() { System out println(Thread currentThread() getId() +
: START TASK + taskName + );
try { Thread sleep(timeLen); } catch (InterruptedException e) {
}
System out println(Thread currentThread() getId() +
: END TASK + taskName + );
}
}
使用此線程池相當簡單 下面兩行代碼初始化線程池
ThreadPool pool = new ThreadPool( ); pool init();
要處理的任務實現ThreadTask 接口即可(如測試代碼里的SimpleTask) 這個接口只有一個方法run()
兩行代碼即可調用
lishixinzhi/Article/program/Java/hx/201311/27203
java不同包裡面的對象可以通用嗎
java
JAVA–不同包類的相互訪問

Charity ice
原創
關注
2點贊·1163人閱讀
1、不同包之間的訪問在定義類之前使用import 加導入的類名;
2、不同包沒有任何關係的兩個類,只有public的類中的public成員才能被另一個包訪問;
package yi;//第一個包
public class A{
public void f(){
System.out.printf(“AAAA!\n”);
}
protected void g(){
System.out.printf(“BBBB!\n”);
}
}
package er;//第二個包
import yi.*;//導入第一個包
class B{
public static void main(String[] agrs){
A aa=new A();
aa.f();//ok
//aa.g();//error
}
}
登錄後複製
3、在不同包有繼承關係的兩個類,只有public類的protected成員和public成員可以被另外一個包的子類內部進行使用,但在子類外部通過子類對象名只能訪問父類的public成員;
package one;
public class A{
protected void f(){
System.out.printf(“AAAA!\n”);
}
public static void main(String[] args){
A aa=new A();
aa.f();
}
}
package two;
import one.*;
class B extends A{
public void g(){
f();
System.out.printf(“BBBB!\n”);
}
}
class TestB{
public static void main(Sring[] args){
B bb=new B();
bb.g();//ok
//bb.f();//error
}
}
java與C語言哪個更有優勢?
現在軟件開發如此熱門,我們在選擇學習語言時,總是不知道到底是選擇java好還是C語言好,它們各自有什麼優缺點?下面昆明IT培訓與大家分享java與C語言哪個更有優勢。
java與C語言優勢對比
java是面向對象的語言,C語言是面向過程的語言,執行效率比C語言低;C語言比java多了指針,不過側面體現了java的健壯性;java多線程機制使程序能夠並行運行,一般用於網絡;安全性java比C語言好,java有垃圾回收機制,C語言沒有,申請的空間需要手動釋放;java通用性好,能夠跨平台直接移植,安裝JVM就行。
java與C語言通用性比較
C語言編程速度要比java快,是由於java必須在虛擬機環境中運行,因此java有平台無關性特點,而C語言要重新修改編譯才可以實現平台的移植;C語言注重算法,java是要用時導包就行;java的基本數據類型,是對對象的引用,C語言也有很多基本類型以及數組以及指針。
java與C語言特徵比較
java面向對象的特徵主要有封裝,繼承,多態;Java能支持方法重載以及重寫;java有修飾符,C語言沒有。java有super關鍵字;java能將類組織起來用Package打包,C語言沒有。
微信有JAVA通用版嗎?
微信官方沒有JAVA通用版。微信版本:iOS版、Android版、MAC版、微信電腦插件版(Windows、Windouwsphone7、Windouwsphone8)、symbian版、BlackBerry版、BlackBerry10版、series40版。
擴展資料
微信版本介紹:
(1)微信支持多種語言,支持Wi-Fi無線局域網、2G,3G和4G移動數據網絡,iOS版,Android版、WindowsPhone版、Blackberry版、諾基亞S40版、S60V3和S60V5版。
(2)微信的最新版本:7.0.4(Android)、7.0.4(iOS)、4.2(Symbian)、5.1.0.0(WindowsPhone8)、1.5(諾基亞S40)、3.0(BlackBerry)、2.0(BlackBerry10)。
(3)微信網頁版:騰訊公司在微信官網上提供網頁版微信,用戶可以通過二維碼掃描登陸微信網頁版與好友溝通交流,亦可使用網頁版傳輸文件等。
(4)企業微信:2016年3月10日,微信官方首次公布“企業微信”的相關細節,並表示將於近一兩個月內發布,引發企業與用戶的廣泛關注。經過一個多月的測試,“企業微信”安卓版正式通過騰訊應用寶首發。
參考資料:微信-按操作系統選擇下載
參考資料:百度百科-微信
原創文章,作者:NOVM,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/139205.html