一个原来的类有播放声音函数如下: public static void PlaySound(int id)
{
}
由于声音时,会有短暂的停顿,想重新开一个线程,但是以前的调用方式不能改变。我这样做的
class SD implements Runnable{
int _id;
SD(int id){
_id = id;
}
public void run() {
cGame.PlaySoundEx(_id);
}
}
public static void PlaySoundEx(int id)
{
......//这里是完全使用的原来函数的功能
}
//把原来的同名函数替换了,然后重新起一个线程完成原来声音函数的调用
public static void PlaySound(int id)
{
new Thread ( new SD(1) ).start();
}
但是编译时有如下错误,请帮忙指正。原来的static属性是不能去掉的。
non-static variable this cannot be referenced from
a static context
sd = new SD(1);
{
}
由于声音时,会有短暂的停顿,想重新开一个线程,但是以前的调用方式不能改变。我这样做的
class SD implements Runnable{
int _id;
SD(int id){
_id = id;
}
public void run() {
cGame.PlaySoundEx(_id);
}
}
public static void PlaySoundEx(int id)
{
......//这里是完全使用的原来函数的功能
}
//把原来的同名函数替换了,然后重新起一个线程完成原来声音函数的调用
public static void PlaySound(int id)
{
new Thread ( new SD(1) ).start();
}
但是编译时有如下错误,请帮忙指正。原来的static属性是不能去掉的。
non-static variable this cannot be referenced from
a static context
sd = new SD(1);
new SD(1)依然是错的,感觉应该方式是指的这里不能建SD的实例。
忘记说了,我的SD是用inner class 实现的。莫非和这个有关?
可是就不理解了,搜索了一下,仿佛有人说静态类不能实例化。更不理解了。
如果要找更多的相关资源google,可以用什么关键词啊。
不搞JAVA好多年,这么基本的也搞不了了;也怪当时基础没有打牢,惭愧啊。
{
cGame c = new cGame();
new Thread ( c.new SD(1) ).start();
}
不行啊。建一次是不可能建第二个的了。
你会发现你的千千静听里面的声音也会一卡一卡的. 主要是 Java 垃圾收集机制 搞的鬼. 收集的时候很耗费资源..所以.原则上还是不好解决的.
因为整个程序运行的都很快,虽然里面也有gc()。只有播声音的时候比较卡,而且声音附近没有gc,当然不排除部分API或许会有相似调用。