给出以下Java代码:
public class Test {
static private class MyThread extends Thread {
private boolean mustShutdown = false;
@Override
public synchronized void run() {
// loop and do nothing, just wait until we must shut down
while (!mustShutdown) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("Exception on wait()");
}
}
}
public synchronized void shutdown() throws InterruptedException {
// set flag for termination, notify the thread and wait for it to die
mustShutdown = true;
notify();
join(); // lock …Run Code Online (Sandbox Code Playgroud) 好的,我正在尝试学习Hadoop和mapreduce.我真的想从mapreduce开始,我发现很多很多简化的映射器和缩减器的例子等等.但是,我看到丢失的东西.
虽然显示一个单词出现次数的示例很容易理解,但它并没有真正帮助我解决任何"现实世界"的问题.有没有人知道在伪现实情况下实现mapreduce的好教程.比方说,我想在类似于Adventureworks的数据存储上使用hadoop和mapreduce.现在我想在5月份获得给定产品的订单.从hadoop/mapreduce的角度看怎么样?(我意识到这可能不是mapreduce旨在解决的问题类型,但它很快就浮现在脑海中.)
任何方向都会有所帮助
如何使用assertEquals查看异常消息是否正确?测试通过,但我不知道它是否达到了正确的错误.
我正在运行的测试.
@Test
public void testTC3()
{
try {
assertEquals("Legal Values: Package Type must be P or R", Shipping.shippingCost('P', -5));
}
catch (Exception e) {
}
}
Run Code Online (Sandbox Code Playgroud)
正在测试的方法.
public static int shippingCost(char packageType, int weight) throws Exception
{
String e1 = "Legal Values: Package Type must be P or R";
String e2 = "Legal Values: Weight < 0";
int cost = 0;
if((packageType != 'P')&&(packageType != 'R'))
{
throw new Exception(e1);
}
if(weight < 0)
{
throw new Exception(e2);
} …Run Code Online (Sandbox Code Playgroud) 问题是关于它变得如此简单,但对于我的生活,我无法在网上找到关于如何创建新服务器的任何地方.不是服务器内部的新数据库,我已经知道如何做到这一点,但我的PC上是一个全新的服务器.我正在使用SQL管理工作室2012.
所以这是本书的引用:
重写方法不得抛出新的或更广泛的已检查异常,而不是重写方法声明的异常.例如,声明FileNotFoundException的方法不能被声明SQLException,Exception或任何其他非运行时异常的方法覆盖,除非它是FileNotFoundException的子类.
现在这是我的问题,如果超类中的方法抛出异常,那么重写方法是否可以抛出异常呢?
因为我刚刚在Java中尝试了这一点,其中重写方法没有抛出任何异常,并且没有错误.
请解释.
我知道有类似这个问题的线程.下面是我的类,我在spring.xml文件中配置它.实际上HumanResourceService是一个只有一个方法的接口.
@Endpoint
public class HolidayEndpoint {
@Autowired
private HumanResourceService humanResourceService;
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
this.humanResourceService = humanResourceService;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在我的spring.xml文件中,当我将HumanResourceService定义为bean时,它无法实例化,因为这是一个接口.如何在spring配置文件中提及接口.我的spring.xml文件如下
<bean id="holidayEndpoint" class="com.mycompany.hr.ws.HolidayEndpoint" autowire="constructor" >
<property name="humanResourceService" ref="humanResourceService" />
</bean>
<bean id="humanResourceService" class="com.mycompany.hr.service.HumanResourceService" />
Run Code Online (Sandbox Code Playgroud) synchronized (Foo.class) {
while (someCondition) {
try {
Foo.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎这个线程在其他一些线程调用时interrupt()或notify()在此线程上都会唤醒.这两者有什么不同吗?
- 编辑 -
我知道一个用于通知一个对象,另一个用于中断一个线程.但是这两者都导致了相同的结果,也就是说,这个线程被唤醒了,所以我想问的是这两种情况的后果是如何相互不同的.
我想让以下代码线程安全.实现它的最佳方法是什么?
private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();
public static final String eventTypeToDateTimeString(long timestamp)
{
return DATE_FORMAT.format(new Date(timestamp));
}
Run Code Online (Sandbox Code Playgroud) 我在java.lang.Object中使用wait()的定时版本,并观察到它在两种不同的场景中的行为不同.
场景1:在Thread中使用run()的默认定义
public static void main (String[] args) throws InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("X");
synchronized(t) { t.wait(10000);}
System.out.print("Y");
}
Run Code Online (Sandbox Code Playgroud)
关于scenario1的问题:我遇到了X和Y之间的延迟.这是因为我从main调用wait()(即使在t上)因此正在使用主线程的调用堆栈,而不是第二个线程?
场景2:动态 子类化线程以覆盖run()以打印内容.
public static void main (String[] args) throws InterruptedException {
Thread t = new Thread() {public void run()
{System.out.print("I am the second thread.");}};
t.start();
System.out.print("X");
synchronized(t) { t.wait(10000);}
System.out.print("Y");
}
Run Code Online (Sandbox Code Playgroud)
关于场景2的问题:我没有遇到任何延迟!是什么改变只是因为我已经覆盖了run()?现在,每次我运行该程序时,它立即打印出"XI am the second thread.Y",无论如何都没有任何延迟!wait()的效果在哪里消失了?
我在不同的博客中读过有关显示器的不同内容.所以我现在有点困惑.
据我所知,monitor是一个确保只有一个线程在关键部分执行代码的人.那么如果我们有3个同步的方法/块,那么我们将有3个监视器来确保只有一个线程在临界区?
如果以上是真的那么为什么说在Java中每个对象都有一个与之关联的监视器?它应该是每个同步块与监视器相关联.
java ×7
wait ×3
exception ×2
notify ×2
hadoop ×1
interruption ×1
join ×1
junit ×1
junit4 ×1
mapreduce ×1
overriding ×1
spring ×1
sql-server ×1
ssms ×1
synchronized ×1