java通用,java通用编码规范

本文目录一览:

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/n/139205.html

(0)
NOVMNOVM
上一篇 2024-10-04
下一篇 2024-10-04

相关推荐

发表回复

登录后才能评论