我已经读过Java线程是用户级线程,用户级线程和内核级线程之间的区别之一是内核级别线程由内核调度(我们无法更改),对于用户级线程,我们可以定义我们的线程自己的调度算法.
那么我们如何在Java中安排线程?在任何给定时间,当准备好执行多个线程时,运行时系统选择Runnable具有最高优先级的线程来执行.如果两个具有相同优先级的线程正在等待CPU,则调度程序选择其中一个以循环方式运行.如果我不想要RR怎么办?有没有办法可以改变它,或者我在这里遗漏了什么?
我得到了java.lang.IllegalMonitorStateException.我提到了这个问题,它解决了我的问题.第一个答案是
To be able to call notify() you need to synchronize on the same object.
synchronized (someObject) {
someObject.wait();
}
/* different thread / object */
synchronized (someObject) {
someObject.notify();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我们需要在同一个对象广告上同步它的工作原理?
据我所知,据我所知
synchronized (someObject) {
someObject.wait();
}
Run Code Online (Sandbox Code Playgroud)
我们得到对象someObject的锁,然后我们调用wait().现在,另一个线程怎么能锁定同一个对象来调用notify()呢?我错过了什么?
据我所知,没有调用Object的序列化类的构造函数,而是第一个非序列化构造函数的no-arg构造函数.现在考虑以下代码
public class SerializeDemo implements Serializable {
private String name;
int age; //default 0
public SerializeDemo(String name, boolean setAge){
this.name = name;
if(setAge){
this.age = 18;
}
}
@Override
public String toString() {
return "Name is " + name + " and age is " + age;
}
public static void main(String args[]) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("//home//aniket//Desktop//serializedObjects.txt")));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("//home//aniket//Desktop//serializedObjects.txt")));
SerializeDemo sd = new SerializeDemo("Test",true);
System.out.println("Before Serialization : " …Run Code Online (Sandbox Code Playgroud) 我需要在buiid.gradle文件附近的java项目中创建文件.我必须在build.gradle文件中创建任务(Groovy任务),我的任务必须在我的项目中的buiid.gradle附近创建文件,但我不知道 - 如何获取buiid.gradle文件的路径,即放入我的项目.
如何通过Groovy获取文件buiid.gradle的完整路径?请帮帮我.
使用Android O开发者预览谷歌已经引入了应该在启动器图标上显示的通知徽章.我正在使用来自开发者频道的Android O模拟器.我写了一个简单的代码来显示通知徽章但它似乎不起作用 -
Notification notification = new Notification.Builder(getApplicationContext())
.chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
.setSmallIcon(android.R.drawable.btn_star)
.setNumber(10)
.build();
mNotificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)
它只显示为正常通知.
API - https://developer.android.com/reference/android/app/Notification.Builder.html#chooseBadgeIcon(int)
还有人为此工作过吗?我错过了什么吗?
在设置中启用了显示徽章.
也试过NotificationChannel.不起作用 -
NotificationChannel mChannel = new NotificationChannel("TestBadge_id", "TestBadgeName", NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setShowBadge(true);
mNotificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(getApplicationContext())
.chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
.setSmallIcon(android.R.drawable.btn_star)
.setNumber(10)
.setChannel("TestBadge_id")
.build();
mNotificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud) 当我这样做时,ls -l | grep ^d它只列出当前目录中的目录.
我想知道的是^ d中的字符是什么意思?
我只是试图创建一个JMS持久订阅者,我正在使用HornetQ服务器.但我得到了执行时抛出的异常
Connection Factory Looked Up : HornetQConnectionFactory [serverLocator=ServerLocatorImpl [initialConnectors=[TransportConfiguration(name=netty, factory=org-hornetq-core-remoting-impl-netty-NettyConnectorFactory) ?port=5445&host=localhost], discoveryGroupConfiguration=null], clientID=null, consumerWindowSize = 1048576, dupsOKBatchSize=1048576, transactionBatchSize=1048576, readOnly=false]
Topic Looked Up : HornetQTopic[TestTopic]
Connection Created : org.hornetq.jms.client.HornetQConnection@299320cf
CLient ID set : DSubCliID
Session Created : HornetQSession->DelegatingSession [session=ClientSessionImpl [name=ab3a1be5-559f-11e3-bd5b-87d0be06d2c5, username=null, closed=false, factory = ClientSessionFactoryImpl [serverLocator=ServerLocatorImpl [initialConnectors=[TransportConfiguration(name=netty, factory=org-hornetq-core-remoting-impl-netty-NettyConnectorFactory) ?port=5445&host=localhost], discoveryGroupConfiguration=null], connectorConfig=TransportConfiguration(name=netty, factory=org-hornetq-core-remoting-impl-netty-NettyConnectorFactory) ?port=5445&host=localhost, backupConfig=null], metaData=(jms-client-id=DSubCliID,jms-session=,)]@22eff179]
javax.jms.JMSSecurityException: HQ119032: User: null doesnt have permission=CREATE_DURABLE_QUEUE on address {2}
at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:388)
at org.hornetq.core.client.impl.ClientSessionImpl.internalCreateQueue(ClientSessionImpl.java:2021)
at org.hornetq.core.client.impl.ClientSessionImpl.createQueue(ClientSessionImpl.java:357)
at org.hornetq.core.client.impl.DelegatingSession.createQueue(DelegatingSession.java:298)
at org.hornetq.jms.client.HornetQSession.createConsumer(HornetQSession.java:782)
at org.hornetq.jms.client.HornetQSession.createDurableConsumer(HornetQSession.java:547)
at org.hornetq.jms.client.HornetQSession.createDurableConsumer(HornetQSession.java:527)
at DurableSubscriber.main(DurableSubscriber.java:38) …Run Code Online (Sandbox Code Playgroud) 考虑下面的示例代码
public class Test {
public static void main(String args[]) {
Test t = new Test();
Class c2 = Test.class;
System.out.println(c2);
}
}
Run Code Online (Sandbox Code Playgroud)
Test.class静态计算并返回编译时Class对象.看一下Test.class它看起来像变量类的语法type java.lang.Class and is static and public.我的问题是这个变量定义在哪里?它在Test类中不存在(因为我没有声明它),它也不在java.lang.Object类中.
我看到了一个类似的方法public final native Class<?> getClass();.这存在于java.lang.Object并且是native java method.此方法返回对象的运行时类.
所以我的问题是这个公共和静态类变量定义在哪里?(如果我错了,请纠正我)是否是一些原生实现?这是在编译时设置的,静态不需要创建类实例.所以即使这是一些本机实现,它是由registerNatives()java.lang.Object中的方法初始化的吗?
我分叉了存储库https://github.com/hornetq/hornetq.git,以便它位于我的用户 id 下https://github.com/aniket91/hornetq.git。然后我将此存储库克隆到我的本地计算机。这样就成功了
[aniket@localhost hornetMq]$ git clone https://github.com/aniket91/hornetq.git
Initialized empty Git repository in /home/aniket/hornetMq/hornetq/.git/
remote: Counting objects: 261664, done.
remote: Compressing objects: 100% (74027/74027), done.
remote: Total 261664 (delta 156514), reused 252367 (delta 147734)
Receiving objects: 100% (261664/261664), 128.22 MiB | 334 KiB/s, done.
Resolving deltas: 100% (156514/156514), done.
Run Code Online (Sandbox Code Playgroud)
现在我想将这个项目导入到我的 Intellij IDEA 中。所以我导入了项目并选择了项目根目录。每次我打开项目时都会出现以下错误
6:03:17 PM Unsupported Git version
The configured version of Git is not supported: 1.7.1.0.
The minimal supported version is 1.7.1.1. Please update.
Run Code Online (Sandbox Code Playgroud)
我已经使用命令行 …
如果我有类似的代码
public static void main(String args[]){
int x = 0;
while (false) { x=3; } //will not compile
}
Run Code Online (Sandbox Code Playgroud)
编译器将投诉x=3无法访问的代码,但如果我有类似的代码
public static void main(String args[]){
int x = 0;
if (false) { x=3; }
for( int i = 0; i< 0; i++) x = 3;
}
Run Code Online (Sandbox Code Playgroud)
然后它通过内部代码正确编译if statement,for loop无法访问.为什么java工作流逻辑没有检测到这种冗余?任何用例?